Use checked addition for allocator implementations (#726)

This commit is contained in:
Philipp Oppermann
2020-01-27 13:25:08 +01:00
committed by GitHub
parent 002d6f255f
commit 3ad5f117c2
2 changed files with 4 additions and 4 deletions

View File

@@ -26,7 +26,7 @@ impl BumpAllocator {
/// memory range is unused. Also, this method must be called only once.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
self.heap_start = heap_start;
self.heap_end = heap_start + heap_size;
self.heap_end = heap_start.saturating_add(heap_size);
self.next = heap_start;
}
}
@@ -36,7 +36,7 @@ unsafe impl GlobalAlloc for Locked<BumpAllocator> {
let mut bump = self.lock(); // get a mutable reference
let alloc_start = align_up(bump.next, layout.align());
let alloc_end = alloc_start + layout.size();
let alloc_end = alloc_start.checked_add(layout.size()).expect("overflow");
if alloc_end > bump.heap_end {
ptr::null_mut() // out of memory