Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 22c83fe

Browse files
committed
Replace an assert! with debug_assert! in u256::shr
The implementation came from the `compiler_builtins` port but this should be weakened to match other integer types.
1 parent d202e8f commit 22c83fe

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/math/support/big.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ impl ops::Shr<u32> for u256 {
109109
type Output = Self;
110110

111111
fn shr(self, rhs: u32) -> Self::Output {
112-
assert!(rhs < Self::BITS, "attempted to shift right with overflow");
112+
debug_assert!(rhs < Self::BITS, "attempted to shift right with overflow");
113+
if rhs >= Self::BITS {
114+
return Self::ZERO;
115+
}
113116

114117
if rhs == 0 {
115118
return self;

src/math/support/big/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,22 @@ fn shr_u128() {
108108
}
109109
assert!(errors.is_empty());
110110
}
111+
112+
#[test]
113+
#[should_panic]
114+
#[cfg(debug_assertions)]
115+
// FIXME(ppc): ppc64le seems to have issues with `should_panic` tests.
116+
#[cfg(not(all(target_arch = "powerpc64", target_endian = "little")))]
117+
fn shr_u256_overflow() {
118+
// Like regular shr, panic on overflow with debug assertions
119+
let _ = u256::MAX >> 256;
120+
}
121+
122+
#[test]
123+
#[cfg(not(debug_assertions))]
124+
fn shr_u256_overflow() {
125+
// No panic without debug assertions
126+
assert_eq!(u256::MAX >> 256, u256::ZERO);
127+
assert_eq!(u256::MAX >> 257, u256::ZERO);
128+
assert_eq!(u256::MAX >> u32::MAX, u256::ZERO);
129+
}

0 commit comments

Comments
 (0)