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 @@ -1158,6 +1158,15 @@ impl<A: Allocator + Clone> RawTableInner<A> {
1158
1158
None => return Err ( fallibility. capacity_overflow ( ) ) ,
1159
1159
} ;
1160
1160
1161
+ // We need an additional check to ensure that the allocation doesn't
1162
+ // exceed `isize::MAX`. We can skip this check on 64-bit systems since
1163
+ // such allocations will never succeed anyways.
1164
+ //
1165
+ // This mirrors what Vec does in the standard library.
1166
+ if mem:: size_of :: < usize > ( ) < 8 && layout. size ( ) > isize:: MAX as usize {
1167
+ return Err ( fallibility. capacity_overflow ( ) ) ;
1168
+ }
1169
+
1161
1170
let ptr: NonNull < u8 > = match do_alloc ( & alloc, layout) {
1162
1171
Ok ( block) => block. cast ( ) ,
1163
1172
Err ( _) => return Err ( fallibility. alloc_err ( layout) ) ,
You can’t perform that action at this time.
0 commit comments