Thread (38 messages) 38 messages, 3 authors, 2025-09-01

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

From: Andreas Hindborg <a.hindborg@kernel.org>
Date: 2025-09-01 08:26:32
Also in: lkml, rust-for-linux

"Daniel Almeida" [off-list ref] writes:
quoted
On 22 Aug 2025, at 09:14, Andreas Hindborg [off-list ref] wrote:
<cut>
quoted
-    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


Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help