Skip to content

Commit 3c9bafa

Browse files
authored
Merge pull request #79 from 00xc/fix_layout_panic
Fix potential panic due to huge layout
2 parents 9645f42 + bead418 commit 3c9bafa

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Diff for: src/hole.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::alloc::Layout;
1+
use core::alloc::{Layout, LayoutError};
22
use core::mem;
33
use core::mem::{align_of, size_of};
44
use core::ptr::null_mut;
@@ -365,13 +365,13 @@ impl HoleList {
365365
/// The [`allocate_first_fit`][HoleList::allocate_first_fit] and
366366
/// [`deallocate`][HoleList::deallocate] methods perform the required alignment
367367
/// themselves, so calling this function manually is not necessary.
368-
pub fn align_layout(layout: Layout) -> Layout {
368+
pub fn align_layout(layout: Layout) -> Result<Layout, LayoutError> {
369369
let mut size = layout.size();
370370
if size < Self::min_size() {
371371
size = Self::min_size();
372372
}
373373
let size = align_up_size(size, mem::align_of::<Hole>());
374-
Layout::from_size_align(size, layout.align()).unwrap()
374+
Layout::from_size_align(size, layout.align())
375375
}
376376

377377
/// Searches the list for a big enough hole.
@@ -389,7 +389,7 @@ impl HoleList {
389389
// release to remove this clippy warning
390390
#[allow(clippy::result_unit_err)]
391391
pub fn allocate_first_fit(&mut self, layout: Layout) -> Result<(NonNull<u8>, Layout), ()> {
392-
let aligned_layout = Self::align_layout(layout);
392+
let aligned_layout = Self::align_layout(layout).map_err(|_| ())?;
393393
let mut cursor = self.cursor().ok_or(())?;
394394

395395
loop {
@@ -419,7 +419,7 @@ impl HoleList {
419419
/// The function performs exactly the same layout adjustments as [`allocate_first_fit`] and
420420
/// returns the aligned layout.
421421
pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout) -> Layout {
422-
let aligned_layout = Self::align_layout(layout);
422+
let aligned_layout = Self::align_layout(layout).unwrap();
423423
deallocate(self, ptr.as_ptr(), aligned_layout.size());
424424
aligned_layout
425425
}

0 commit comments

Comments
 (0)