[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250815-rnull-up-v6-16-v5-6-581453124c15@kernel.org>
Date: Fri, 15 Aug 2025 09:30:41 +0200
From: Andreas Hindborg <a.hindborg@...nel.org>
To: Boqun Feng <boqun.feng@...il.com>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>,
Jens Axboe <axboe@...nel.dk>, Breno Leitao <leitao@...ian.org>
Cc: linux-block@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org, Andreas Hindborg <a.hindborg@...nel.org>
Subject: [PATCH v5 06/18] rust: str: add `bytes_to_bool` helper function
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@...nel.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
Powered by blists - more mailing lists