From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Younes Akhouayri via B4 Relay" <devnull+git.younes.io@kernel.org>
Cc: git@younes.io, "Yury Norov" <yury.norov@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Onur Özkan" <work@onurozkan.dev>,
rust-for-linux@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v3] rust: num: restrict bool conversion to unsigned Bounded
Date: Fri, 21 Aug 2026 21:02:45 +0900 [thread overview]
Message-ID: <DKULUOXLCWCH.6RUE2BRSAOCT@nvidia.com> (raw)
In-Reply-To: <20260815-fix-rust-bounded-from-bool-submit-v3-1-fdca23008397@younes.io>
On Sun Aug 16, 2026 at 5:12 AM JST, Younes Akhouayri via B4 Relay wrote:
> From: Younes Akhouayri <git@younes.io>
>
> From<bool> turns true into 1. A signed Bounded with N = 1 can hold
> only -1 and 0. The current implementation can therefore create a value
> that breaks Bounded's invariant. Deref relies on that invariant and
> calls unreachable_unchecked() when it is broken, so safe Rust can reach
> undefined behavior.
>
> The other primitive conversions require the source and destination to
> have the same signedness. Treat bool as an unsigned one-bit value and
> allow this conversion only for unsigned Bounded types.
>
> Fixes: 01e345e82ec3 ("rust: num: add Bounded integer wrapping type")
> Closes: https://lore.kernel.org/rust-for-linux/OzuVxu0--J-9@younes.io/
> Cc: stable@vger.kernel.org
> Assisted-by: Codex:gpt-5.6-sol
> Signed-off-by: Younes Akhouayri <git@younes.io>
Thanks, I think this looks good and limiting the conversion to unsigneds
is reasonable. If a signed Bounded is needed, the caller can always
perform another conversion step.
Just one question below.
> ---
> Only allow From<bool> for unsigned Bounded types.
> ---
> Changes in v3:
> - Restrict From<bool> to unsigned Bounded types, matching the other
> primitive conversions.
> - Remove the BoolFits helper and signed-width list.
> - Link to v2: https://lore.kernel.org/rust-for-linux/20260815-fix-rust-bounded-from-bool-submit-v2-1-aa35bb2c22d1@younes.io/
>
> Changes in v2:
> - Use vertical formatting for the nested `num` import.
> - Link to v1: https://lore.kernel.org/rust-for-linux/20260815-fix-rust-bounded-from-bool-submit-v1-1-882227bf40ba@younes.io/
> ---
> rust/kernel/num/bounded.rs | 20 ++++++++++++++------
> 1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
> index d192610a687d..f2413dc7458b 100644
> --- a/rust/kernel/num/bounded.rs
> +++ b/rust/kernel/num/bounded.rs
> @@ -13,7 +13,10 @@
> };
>
> use kernel::{
> - num::Integer,
> + num::{
> + Integer,
> + Unsigned, //
> + },
> prelude::*, //
> };
>
> @@ -174,13 +177,16 @@ fn fits_within<T: Integer>(value: T, num_bits: u32) -> bool {
> /// // `u8` (regardless of the passed value).
> /// // let _ = Bounded::<u32, 6>::from(10u8);
> ///
> -/// // Booleans can be converted into single-bit `Bounded`s.
> +/// // Booleans can be converted into unsigned `Bounded`s.
> ///
> /// let v = Bounded::<u64, 1>::from(false);
> /// assert_eq!(v.get(), 0);
> ///
> /// let v = Bounded::<u64, 1>::from(true);
> /// assert_eq!(v.get(), 1);
> +///
> +/// // This does not build because `i8` is signed.
> +/// // let _ = Bounded::<i8, 2>::from(true);
> /// ```
> ///
> /// Infallible conversions from a [`Bounded`] to a primitive integer are also supported, and
> @@ -1109,7 +1115,7 @@ fn from(value: Bounded<T, N>) -> $type {
> i8 i16 i32 i64 isize
> );
>
> -// Single-bit `Bounded`s can be converted from/to a boolean.
> +// Single-bit `Bounded`s can be converted to a boolean.
Since the `From` direction is now limited to unsigned `Bounded`s, maybe
this conversion should as well?
prev parent reply other threads:[~2026-08-21 12:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-15 20:12 [PATCH v3] rust: num: restrict bool conversion to unsigned Bounded Younes Akhouayri
2026-08-15 20:12 ` Younes Akhouayri via B4 Relay
2026-08-21 12:02 ` Alexandre Courbot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DKULUOXLCWCH.6RUE2BRSAOCT@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=devnull+git.younes.io@kernel.org \
--cc=gary@garyguo.net \
--cc=git@younes.io \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
--cc=yury.norov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.