Skip to contentSkip to conversation

FAILED: patch "[PATCH] rust: num: reject Bounded::shr overshifts at build time" failed to apply to 7.2-stable tree

PATCH Applied [email protected] 3 participants 3 messages Sep 3 – Sep 4

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 7.2-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

To reproduce the conflict and resubmit, you may use the following commands:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-7.2.y
git checkout FETCH_HEAD
git cherry-pick -x 223aa25aee82e188ddf043a8703b16e5fdfc37d8
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026090332-lazy-chirpy-4151@gregkh' --subject-prefix 'PATCH 7.2.y' 'HEAD^..'

Possible dependencies:

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 223aa25aee82e188ddf043a8703b16e5fdfc37d8 Mon Sep 17 00:00:00 2001
From: Eliot Courtney <[email protected]>
Date: Mon, 10 Aug 2026 22:55:24 +0900
Subject: [PATCH] rust: num: reject Bounded::shr overshifts at build time

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.

[ This implies we can break the type invariant, which in turn means
we can trigger UB via Deref, e.g.:

rust_kernel: panicked at rust/kernel/num/bounded.rs:528:22:
unsafe precondition(s) violated: hint::unreachable_unchecked must never be reached

- Miguel ]

rust/kernel/num/bounded.rs +1 -0
index 9ad7df1a243d..90483d2c5374 100644
@@ -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
maintainer
#1 lore raw

PATCH 1/1 rust: num: reject Bounded::shr overshifts at build time

From: Eliot Courtney <[email protected]>

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.

[ This implies we can break the type invariant, which in turn means
we can trigger UB via Deref, e.g.:

rust_kernel: panicked at rust/kernel/num/bounded.rs:528:22:
unsafe precondition(s) violated: hint::unreachable_unchecked must never be reached

- Miguel ]

Fixes c59a2d14cd24 ("rust: num: add `shr` and `shl` methods to `Bounded`")
(cherry picked from commit 223aa25aee82e188ddf043a8703b16e5fdfc37d8)
---
 rust/kernel/num/bounded.rs | 1 +
 1 file changed, 1 insertion(+)
rust/kernel/num/bounded.rs +1 -0
index dafe77782d79..92a17d5aec32 100644
@@ -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
Signature · 1 line
2.55.0
maintainer Applied
#2 lore raw

Re: [PATCH 7.2.y] rust: num: reject Bounded::shr overshifts at build time

Quoted 2 lines

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.

Queued for 7.2, thanks.

Signature · 2 lines
Thanks,
Sasha