File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -452,6 +452,10 @@ impl<T> HeaderMap<T> {
452
452
/// allocations before `capacity` headers are stored in the map.
453
453
///
454
454
/// More capacity than requested may be allocated.
455
+ ///
456
+ /// # Panics
457
+ ///
458
+ /// Requested capacity too large: would overflow `usize`.
455
459
///
456
460
/// # Examples
457
461
///
@@ -472,7 +476,13 @@ impl<T> HeaderMap<T> {
472
476
danger : Danger :: Green ,
473
477
}
474
478
} else {
475
- let raw_cap = to_raw_capacity ( capacity) . next_power_of_two ( ) ;
479
+ let raw_cap = match to_raw_capacity ( capacity) . checked_next_power_of_two ( ) {
480
+ Some ( c) => c,
481
+ None => panic ! (
482
+ "requested capacity {} too large: next power of two would overflow `usize`" ,
483
+ capacity
484
+ ) ,
485
+ } ;
476
486
assert ! ( raw_cap <= MAX_SIZE , "requested capacity too large" ) ;
477
487
debug_assert ! ( raw_cap > 0 ) ;
478
488
@@ -3218,7 +3228,13 @@ fn usable_capacity(cap: usize) -> usize {
3218
3228
3219
3229
#[ inline]
3220
3230
fn to_raw_capacity ( n : usize ) -> usize {
3221
- n + n / 3
3231
+ match n. checked_add ( n / 3 ) {
3232
+ Some ( n) => n,
3233
+ None => panic ! (
3234
+ "requested capacity {} too large: overflow while converting to raw capacity" ,
3235
+ n
3236
+ ) ,
3237
+ }
3222
3238
}
3223
3239
3224
3240
#[ inline]
You can’t perform that action at this time.
0 commit comments