Skip to content

Commit 8fe8f9f

Browse files
committed
Guard against allocations exceeding isize::MAX
Fixes #264
1 parent 805b5e2 commit 8fe8f9f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/map.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4644,12 +4644,16 @@ mod test_map {
46444644
panic!("usize::MAX should trigger an overflow!");
46454645
}
46464646

4647-
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE / 8) {
4647+
if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE / 16) {
46484648
} else {
46494649
// 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.
46514651
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) {
46534657
} else {
46544658
panic!("usize::MAX / 8 should trigger an OOM!");
46554659
}

src/raw/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,15 @@ impl<A: Allocator + Clone> RawTableInner<A> {
11581158
None => return Err(fallibility.capacity_overflow()),
11591159
};
11601160

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+
11611170
let ptr: NonNull<u8> = match do_alloc(&alloc, layout) {
11621171
Ok(block) => block.cast(),
11631172
Err(_) => return Err(fallibility.alloc_err(layout)),

0 commit comments

Comments
 (0)