[PATCH 2/2] rust: kernel: document safety for as_str_unchecked

Navaneeth K posted 2 patches 1 week, 2 days ago
[PATCH 2/2] rust: kernel: document safety for as_str_unchecked
Posted by Navaneeth K 1 week, 2 days ago
Replace the TODO safety comment with a proper explanation.
The safety of from_utf8_unchecked relies on the caller guaranteeing
that the input bytes are valid UTF-8.

Signed-off-by: Navaneeth K <knavaneeth786@gmail.com>
---
 rust/kernel/str.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 5c74e5f77601..2801388c6bd1 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -385,7 +385,7 @@ pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
     /// ```
     #[inline]
     pub unsafe fn as_str_unchecked(&self) -> &str {
-        // SAFETY: TODO.
+        // SAFETY: The caller guarantees that the contents are valid UTF-8.
         unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
     }
 
-- 
2.43.0
Re: [PATCH 2/2] rust: kernel: document safety for as_str_unchecked
Posted by Alice Ryhl 1 week ago
On Sat, Nov 22, 2025 at 07:09:08PM +0000, Navaneeth K wrote:
> Replace the TODO safety comment with a proper explanation.
> The safety of from_utf8_unchecked relies on the caller guaranteeing
> that the input bytes are valid UTF-8.
> 
> Signed-off-by: Navaneeth K <knavaneeth786@gmail.com>
> ---
>  rust/kernel/str.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 5c74e5f77601..2801388c6bd1 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -385,7 +385,7 @@ pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
>      /// ```
>      #[inline]
>      pub unsafe fn as_str_unchecked(&self) -> &str {
> -        // SAFETY: TODO.
> +        // SAFETY: The caller guarantees that the contents are valid UTF-8.
>          unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }

As far as I know another patch is deleting this method, so I don't think
there is anything to fix anymore.

Alice