This repository was archived by the owner on Apr 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments