[PATCH v2 02/12] rust: num: reject Bounded::shr overshifts at build time
From: Eliot Courtney
Date: Mon Aug 10 2026 - 09:58:16 EST
Make `shr` reject shifts of at least the type's bit width at build
time, instead of panicking or masking the shift amount at runtime.
Signed-off-by: Eliot Courtney <ecourtney@xxxxxxxxxx>
Acked-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
---
rust/kernel/num/bounded.rs | 1 +
1 file changed, 1 insertion(+)
diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 9ad7df1a243d..90483d2c5374 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -485,6 +485,7 @@ pub fn cast<U>(self) -> Bounded<U, N>
/// assert_eq!(v_shifted.get(), 0xff);
/// ```
pub fn shr<const SHIFT: u32, const RES: u32>(self) -> Bounded<T, RES> {
+ const_assert!(SHIFT < T::BITS);
const_assert!(RES + SHIFT >= N);
// SAFETY: We shift the value right by `SHIFT`, reducing the number of bits needed to
--
2.55.0