Don't panic on overflow in allocator; return null pointer instead (#738)

This commit is contained in:
Philipp Oppermann
2020-02-04 09:47:39 +01:00
committed by GitHub
parent 9fb6c1d0bd
commit 3a6d3153a4
2 changed files with 5 additions and 2 deletions

View File

@@ -36,7 +36,10 @@ unsafe impl GlobalAlloc for Locked<BumpAllocator> {
let mut bump = self.lock(); // get a mutable reference let mut bump = self.lock(); // get a mutable reference
let alloc_start = align_up(bump.next, layout.align()); let alloc_start = align_up(bump.next, layout.align());
let alloc_end = alloc_start.checked_add(layout.size()).expect("overflow"); let alloc_end = match alloc_start.checked_add(layout.size()) {
Some(end) => end,
None => return ptr::null_mut(),
};
if alloc_end > bump.heap_end { if alloc_end > bump.heap_end {
ptr::null_mut() // out of memory ptr::null_mut() // out of memory

View File

@@ -86,7 +86,7 @@ impl LinkedListAllocator {
/// Returns the allocation start address on success. /// Returns the allocation start address on success.
fn alloc_from_region(region: &ListNode, size: usize, align: usize) -> Result<usize, ()> { fn alloc_from_region(region: &ListNode, size: usize, align: usize) -> Result<usize, ()> {
let alloc_start = align_up(region.start_addr(), align); let alloc_start = align_up(region.start_addr(), align);
let alloc_end = alloc_start.checked_add(size).expect("overflow"); let alloc_end = alloc_start.checked_add(size).ok_or(())?;
if alloc_end > region.end_addr() { if alloc_end > region.end_addr() {
// region too small // region too small