]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
rust: num: bounded: mark __new as unsafe
authorHsiu Che Yu <yu.whisper.personal@gmail.com>
Thu, 4 Dec 2025 03:38:48 +0000 (11:38 +0800)
committerMiguel Ojeda <ojeda@kernel.org>
Tue, 6 Jan 2026 20:01:47 +0000 (21:01 +0100)
commit3a1ec424dd9c9491138a5ebadb24ce9f33e6a822
treeca3707f0850dab827f0f990d7d27de5feb20eb0e
parent609db7e73b3ecc6a0b44dc6486e88e4bce6fd8c0
rust: num: bounded: mark __new as unsafe

The `Bounded::__new()` constructor relies on the caller to ensure the
value can be represented within N bits. Failing to uphold this
requirement breaks the type invariant. Mark it as unsafe and document
this requirement in a Safety section to make the contract explicit.

Update all call sites to use unsafe blocks and change their comments
from `INVARIANT:` to `SAFETY:`, as they are now justifying unsafe
operations rather than establishing type invariants.

Fixes: 01e345e82ec3a ("rust: num: add Bounded integer wrapping type")
Link: https://lore.kernel.org/all/aS1qC_ol2XEpZ44b@google.com/
Reported-by: Miguel Ojeda <ojeda@kernel.org>
Closes: https://github.com/Rust-for-Linux/linux/issues/1211
Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>
Acked-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20251204033849.23480-1-yu.whisper.personal@gmail.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/num/bounded.rs