[PATCH v5 06/18] rust: str: add `bytes_to_bool` helper function

Andreas Hindborg posted 18 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v5 06/18] rust: str: add `bytes_to_bool` helper function
Posted by Andreas Hindborg 1 month, 2 weeks ago
Add a convenience function to convert byte slices to boolean values by
wrapping them in a null-terminated C string and delegating to the
existing `kstrtobool` function. Only considers the first two bytes of
the input slice, following the kernel's boolean parsing semantics.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/str.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 5611f7846dc0..ced1cb639efc 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -978,6 +978,16 @@ pub fn kstrtobool(string: &CStr) -> Result<bool> {
     kernel::error::to_result(ret).map(|()| result)
 }
 
+/// Convert `&[u8]` to `bool` by deferring to [`kernel::str::kstrtobool`].
+///
+/// Only considers at most the first two bytes of `bytes`.
+pub fn bytes_to_bool(bytes: &[u8]) -> Result<bool> {
+    // `ktostrbool` only considers the first two bytes of the input.
+    let nbuffer = [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or(&0), 0];
+    let c_str = CStr::from_bytes_with_nul(nbuffer.split_inclusive(|c| *c == 0).next().unwrap())?;
+    kstrtobool(c_str)
+}
+
 /// An owned string that is guaranteed to have exactly one `NUL` byte, which is at the end.
 ///
 /// Used for interoperability with kernel APIs that take C strings.

-- 
2.47.2
Re: [PATCH v5 06/18] rust: str: add `bytes_to_bool` helper function
Posted by Alice Ryhl 1 month, 2 weeks ago
On Fri, Aug 15, 2025 at 09:30:41AM +0200, Andreas Hindborg wrote:
> Add a convenience function to convert byte slices to boolean values by
> wrapping them in a null-terminated C string and delegating to the
> existing `kstrtobool` function. Only considers the first two bytes of
> the input slice, following the kernel's boolean parsing semantics.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
>  rust/kernel/str.rs | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 5611f7846dc0..ced1cb639efc 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -978,6 +978,16 @@ pub fn kstrtobool(string: &CStr) -> Result<bool> {
>      kernel::error::to_result(ret).map(|()| result)
>  }
>  
> +/// Convert `&[u8]` to `bool` by deferring to [`kernel::str::kstrtobool`].
> +///
> +/// Only considers at most the first two bytes of `bytes`.
> +pub fn bytes_to_bool(bytes: &[u8]) -> Result<bool> {
> +    // `ktostrbool` only considers the first two bytes of the input.
> +    let nbuffer = [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or(&0), 0];
> +    let c_str = CStr::from_bytes_with_nul(nbuffer.split_inclusive(|c| *c == 0).next().unwrap())?;
> +    kstrtobool(c_str)
> +}

Ouch. That's unpleasant. I would probably suggest this instead to avoid
the length computation:

/// # Safety
/// `string` is a readable NUL-terminated string
unsafe fn kstrtobool_raw(string: *const c_char) -> Result<bool> {
    let mut result: bool = false;
    let ret = unsafe { bindings::kstrtobool(string, &raw mut result) };
    kernel::error::to_result(ret).map(|()| result)
}

pub fn kstrtobool(string: &CStr) -> Result<bool> {
    // SAFETY: Caller ensures that `string` is NUL-terminated.
    unsafe { kstrtobool_cstr(string.as_char_ptr()) }
}

pub fn kstrtobool_bytes(string: &[u8]) -> Result<bool> {
    let mut stack_string = [0u8; 3];

    if let Some(first) = string.get(0) {
        stack_string[0] = *first;
    }
    if let Some(second) = string.get(1) {
        stack_string[1] = *second;
    }

    // SAFETY: stack_string[2] is zero, so the string is NUL-terminated.
    unsafe { kstrtobool_cstr(stack_string.as_ptr()) }
}

Alice
Re: [PATCH v5 06/18] rust: str: add `bytes_to_bool` helper function
Posted by Andreas Hindborg 1 month, 2 weeks ago
"Alice Ryhl" <aliceryhl@google.com> writes:

> On Fri, Aug 15, 2025 at 09:30:41AM +0200, Andreas Hindborg wrote:
>> Add a convenience function to convert byte slices to boolean values by
>> wrapping them in a null-terminated C string and delegating to the
>> existing `kstrtobool` function. Only considers the first two bytes of
>> the input slice, following the kernel's boolean parsing semantics.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>>  rust/kernel/str.rs | 10 ++++++++++
>>  1 file changed, 10 insertions(+)
>>
>> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
>> index 5611f7846dc0..ced1cb639efc 100644
>> --- a/rust/kernel/str.rs
>> +++ b/rust/kernel/str.rs
>> @@ -978,6 +978,16 @@ pub fn kstrtobool(string: &CStr) -> Result<bool> {
>>      kernel::error::to_result(ret).map(|()| result)
>>  }
>>
>> +/// Convert `&[u8]` to `bool` by deferring to [`kernel::str::kstrtobool`].
>> +///
>> +/// Only considers at most the first two bytes of `bytes`.
>> +pub fn bytes_to_bool(bytes: &[u8]) -> Result<bool> {
>> +    // `ktostrbool` only considers the first two bytes of the input.
>> +    let nbuffer = [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or(&0), 0];
>> +    let c_str = CStr::from_bytes_with_nul(nbuffer.split_inclusive(|c| *c == 0).next().unwrap())?;
>> +    kstrtobool(c_str)
>> +}
>
> Ouch. That's unpleasant. I would probably suggest this instead to avoid
> the length computation:
>
> /// # Safety
> /// `string` is a readable NUL-terminated string
> unsafe fn kstrtobool_raw(string: *const c_char) -> Result<bool> {
>     let mut result: bool = false;
>     let ret = unsafe { bindings::kstrtobool(string, &raw mut result) };
>     kernel::error::to_result(ret).map(|()| result)
> }
>
> pub fn kstrtobool(string: &CStr) -> Result<bool> {
>     // SAFETY: Caller ensures that `string` is NUL-terminated.
>     unsafe { kstrtobool_cstr(string.as_char_ptr()) }
> }
>
> pub fn kstrtobool_bytes(string: &[u8]) -> Result<bool> {
>     let mut stack_string = [0u8; 3];
>
>     if let Some(first) = string.get(0) {

Clippy will complain about `string.get(0)` suggesting `string.first()`.

>         stack_string[0] = *first;
>     }
>     if let Some(second) = string.get(1) {
>         stack_string[1] = *second;
>     }

I don't really think this procedural assignment is better or worse than assigning
at declaration.

>
>     // SAFETY: stack_string[2] is zero, so the string is NUL-terminated.
>     unsafe { kstrtobool_cstr(stack_string.as_ptr()) }

I'll split it up.


Best regards,
Andreas Hindborg