Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential panic due to huge layout #79

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/hole.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::alloc::Layout;
use core::alloc::{Layout, LayoutError};
use core::mem;
use core::mem::{align_of, size_of};
use core::ptr::null_mut;
Expand Down Expand Up @@ -365,13 +365,13 @@ impl HoleList {
/// The [`allocate_first_fit`][HoleList::allocate_first_fit] and
/// [`deallocate`][HoleList::deallocate] methods perform the required alignment
/// themselves, so calling this function manually is not necessary.
pub fn align_layout(layout: Layout) -> Layout {
pub fn align_layout(layout: Layout) -> Result<Layout, LayoutError> {
let mut size = layout.size();
if size < Self::min_size() {
size = Self::min_size();
}
let size = align_up_size(size, mem::align_of::<Hole>());
Layout::from_size_align(size, layout.align()).unwrap()
Layout::from_size_align(size, layout.align())
}

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

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