Skip to content

Commit

Permalink
Assert in HoleList::new that the space is large enough to store a `…
Browse files Browse the repository at this point in the history
…Hole`

Ensures that no out of bounds write happens when the given hole size is to small, or becomes too small after alignment.
  • Loading branch information
phil-opp committed Sep 1, 2022
1 parent c6956c6 commit 8efb4b5
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/hole.rs
Expand Up @@ -329,14 +329,22 @@ impl HoleList {
/// The pointer to `hole_addr` is automatically aligned.
pub unsafe fn new(hole_addr: *mut u8, hole_size: usize) -> HoleList {
assert_eq!(size_of::<Hole>(), Self::min_size());
assert!(hole_size >= size_of::<Hole>());

let aligned_hole_addr = align_up(hole_addr, align_of::<Hole>());
let aligned_hole_size = hole_size - ((aligned_hole_addr as usize) - (hole_addr as usize));
assert!(aligned_hole_size >= size_of::<Hole>());

let ptr = aligned_hole_addr as *mut Hole;
ptr.write(Hole {
size: hole_size - ((aligned_hole_addr as usize) - (hole_addr as usize)),
size: aligned_hole_size,
next: None,
});

assert_eq!(
hole_addr.wrapping_add(hole_size),
aligned_hole_addr.wrapping_add(aligned_hole_size)
);
HoleList {
first: Hole {
size: 0,
Expand Down

0 comments on commit 8efb4b5

Please sign in to comment.