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

From: Andreas Hindborg
Date: Mon Sep 01 2025 - 04:26:59 EST


"Daniel Almeida" <daniel.almeida@xxxxxxxxxxxxx> writes:

>> On 22 Aug 2025, at 09:14, Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:

<cut>

>> - 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 kstrtobool_bytes(bytes: &[u8]) -> Result<bool> {
>> + // `ktostrbool` only considers the first two bytes of the input.
>> + let stack_string = [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or(&0), 0];
>
> Can’t this be CStr::from_bytes_with_nul() ?
>
> This means that kstrtobool_raw could take a &CStr directly and thus not be unsafe IIUC?

By design, the input to this function need not be null terminated. My
use case is parsing the contents of a configfs file, and I would not
want to change the contents of the file, or allocate to create a null
terminated string, before calling this method.

We could add another function `kstrtobool_cstr` to do what you are
asking, but I think that could be a separate patch.


Best regards,
Andreas Hindborg