File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -4644,12 +4644,16 @@ mod test_map {
4644
4644
panic ! ( "usize::MAX should trigger an overflow!" ) ;
4645
4645
}
4646
4646
4647
- if let Err ( AllocError { .. } ) = empty_bytes. try_reserve ( MAX_USIZE / 8 ) {
4647
+ if let Err ( AllocError { .. } ) = empty_bytes. try_reserve ( MAX_USIZE / 16 ) {
4648
4648
} else {
4649
4649
// This may succeed if there is enough free memory. Attempt to
4650
- // allocate a second hashmap to ensure the allocation will fail.
4650
+ // allocate a few more hashmaps to ensure the allocation will fail.
4651
4651
let mut empty_bytes2: HashMap < u8 , u8 > = HashMap :: new ( ) ;
4652
- if let Err ( AllocError { .. } ) = empty_bytes2. try_reserve ( MAX_USIZE / 8 ) {
4652
+ let _ = empty_bytes2. try_reserve ( MAX_USIZE / 16 ) ;
4653
+ let mut empty_bytes3: HashMap < u8 , u8 > = HashMap :: new ( ) ;
4654
+ let _ = empty_bytes3. try_reserve ( MAX_USIZE / 16 ) ;
4655
+ let mut empty_bytes4: HashMap < u8 , u8 > = HashMap :: new ( ) ;
4656
+ if let Err ( AllocError { .. } ) = empty_bytes4. try_reserve ( MAX_USIZE / 16 ) {
4653
4657
} else {
4654
4658
panic ! ( "usize::MAX / 8 should trigger an OOM!" ) ;
4655
4659
}
Original file line number Diff line number Diff line change @@ -1160,6 +1160,15 @@ impl<A: Allocator + Clone> RawTableInner<A> {
1160
1160
None => return Err ( fallibility. capacity_overflow ( ) ) ,
1161
1161
} ;
1162
1162
1163
+ // We need an additional check to ensure that the allocation doesn't
1164
+ // exceed `isize::MAX`. We can skip this check on 64-bit systems since
1165
+ // such allocations will never succeed anyways.
1166
+ //
1167
+ // This mirrors what Vec does in the standard library.
1168
+ if mem:: size_of :: < usize > ( ) < 8 && layout. size ( ) > isize:: MAX as usize {
1169
+ return Err ( fallibility. capacity_overflow ( ) ) ;
1170
+ }
1171
+
1163
1172
let ptr: NonNull < u8 > = match do_alloc ( & alloc, layout) {
1164
1173
Ok ( block) => block. cast ( ) ,
1165
1174
Err ( _) => return Err ( fallibility. alloc_err ( layout) ) ,
You can’t perform that action at this time.
0 commit comments