File tree Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Original file line number Diff line number Diff line change @@ -2743,12 +2743,16 @@ impl BigUint {
2743
2743
/// Note that setting a bit greater than the current bit length, a reallocation may be needed
2744
2744
/// to store the new digits
2745
2745
pub fn set_bit ( & mut self , bit : u64 , value : bool ) {
2746
+ // Note: we're saturating `digit_index` and `new_len` -- any such case is guaranteed to
2747
+ // fail allocation, and that's more consistent than adding our own overflow panics.
2746
2748
let bits_per_digit = u64:: from ( big_digit:: BITS ) ;
2747
- let digit_index = ( bit / bits_per_digit) . to_usize ( ) . unwrap ( ) ;
2749
+ let digit_index = ( bit / bits_per_digit)
2750
+ . to_usize ( )
2751
+ . unwrap_or ( core:: usize:: MAX ) ;
2748
2752
let bit_mask = ( 1 as BigDigit ) << ( bit % bits_per_digit) ;
2749
2753
if value {
2750
2754
if digit_index >= self . data . len ( ) {
2751
- let new_len = digit_index. checked_add ( 1 ) . unwrap ( ) ;
2755
+ let new_len = digit_index. saturating_add ( 1 ) ;
2752
2756
self . data . resize ( new_len, 0 ) ;
2753
2757
}
2754
2758
self . data [ digit_index] |= bit_mask;
You can’t perform that action at this time.
0 commit comments