Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: str: improve safety comment for CString::try_from_fmt

Improve the safety comment for the `inc_len()` call in
`CString::try_from_fmt()` to clarify why `bytes_written()` is
guaranteed not to exceed the buffer capacity.

The current comment states that bytes written is bounded by size,
but does not explain that this invariant is maintained because:
1. The `Formatter` is created with `size` as its capacity limit
2. The `?` operators on `write_fmt` and `write_str` ensure early
return if writing exceeds this limit

Suggested-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/rust-for-linux/20221114145329.0f47a3ab@GaryWorkstation/
Link: https://github.com/Rust-for-Linux/linux/issues/936
Signed-off-by: Nakamura Shuta <nakamura.shuta@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Link: https://patch.msgid.link/20260119062925.1647-1-nakamura.shuta@gmail.com
[ Updated tags: it was a suggestion from Gary from the mailing list
(the linked issue is mostly about adding a `debug_assert_eq!`).
- Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Nakamura Shuta and committed by
Miguel Ojeda
bf074eb6 79e25710

+4 -1
+4 -1
rust/kernel/str.rs
··· 844 844 f.write_str("\0")?; 845 845 846 846 // SAFETY: The number of bytes that can be written to `f` is bounded by `size`, which is 847 - // `buf`'s capacity. The contents of the buffer have been initialised by writes to `f`. 847 + // `buf`'s capacity. The `Formatter` is created with `size` as its limit, and the `?` 848 + // operators on `write_fmt` and `write_str` above ensure that if writing exceeds this 849 + // limit, an error is returned early. The contents of the buffer have been initialised 850 + // by writes to `f`. 848 851 unsafe { buf.inc_len(f.bytes_written()) }; 849 852 850 853 // Check that there are no `NUL` bytes before the end.