Skip to content

Commit d4175c8

Browse files
authored
Merge pull request #149 from Sieger9303/fix-alloc-ringbuffer-large-allocation
fix: reject excessive AllocRingBuffer allocations
2 parents c6ab31c + 4e6e871 commit d4175c8

5 files changed

Lines changed: 10 additions & 46 deletions

File tree

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ license = "MIT"
1515

1616
[dev-dependencies]
1717
criterion = { version = "0.4.0", features = ["html_reports"] }
18-
compiletest_rs = "0.10.0"
1918

2019
[features]
2120
default = ["alloc"]

‎src/with_alloc/alloc_ringbuffer.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,16 @@ impl<T> AllocRingBuffer<T> {
321321
#[must_use]
322322
pub fn new(capacity: usize) -> Self {
323323
assert_ne!(capacity, 0, "Capacity must be greater than 0");
324-
let size = capacity.next_power_of_two();
325-
let layout = alloc::alloc::Layout::array::<T>(size).unwrap();
326-
let buf = unsafe { alloc::alloc::alloc(layout).cast() };
324+
let size = capacity
325+
.checked_next_power_of_two()
326+
.expect("Capacity is too large");
327+
let layout = alloc::alloc::Layout::array::<T>(size).expect("Capacity is too large");
328+
#[cfg(target_pointer_width = "64")]
329+
assert!(layout.size() <= (1usize << 40), "Capacity is too large");
330+
let buf: *mut T = unsafe { alloc::alloc::alloc(layout).cast() };
331+
if buf.is_null() {
332+
alloc::alloc::handle_alloc_error(layout);
333+
}
327334
Self {
328335
buf,
329336
size,

‎tests/compile-fail/test_const_generic_array_zero_length.rs‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎tests/compile-fail/test_const_generic_array_zero_length_new.rs‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎tests/compiletests.rs‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)