mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Don't panic on overflow in allocator; return null pointer instead (#738)
This commit is contained in:
committed by
GitHub
parent
9fb6c1d0bd
commit
3a6d3153a4
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user