Skip to content

Commit 7da0533

Browse files
committed
Panic when trying to extend an unitialized Heap
1 parent ede19c8 commit 7da0533

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/hole.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ impl HoleList {
454454
}
455455

456456
pub(crate) unsafe fn extend(&mut self, by: usize) {
457+
assert!(!self.top.is_null(), "tried to extend an empty heap");
458+
457459
let top = self.top;
458460

459461
let dead_space = top.align_offset(align_of::<Hole>());
@@ -810,4 +812,10 @@ pub mod test {
810812
// available size is too small to store a hole -> it should panic
811813
unsafe { HoleList::new(heap_start.sub(1), 2 * core::mem::size_of::<usize>()) };
812814
}
815+
816+
#[test]
817+
#[should_panic]
818+
fn extend_empty() {
819+
unsafe { HoleList::empty().extend(16) };
820+
}
813821
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ impl Heap {
246246
/// at least `2 * size_of::<usize>`, keeping the amount a multiple of
247247
/// `size_of::<usize>`.
248248
///
249+
/// Calling this method on an uninitialized Heap will panic.
250+
///
249251
/// # Safety
250252
///
251253
/// The amount of data given in `by` MUST exist directly after the original
@@ -255,8 +257,6 @@ impl Heap {
255257
/// Even if this operation doesn't increase the [usable size][`Self::size`]
256258
/// by exactly `by` bytes, those bytes are still owned by the Heap for
257259
/// later use.
258-
///
259-
/// Calling this method on an uninitialized Heap is undefined behavior.
260260
pub unsafe fn extend(&mut self, by: usize) {
261261
self.holes.extend(by);
262262
}

0 commit comments

Comments
 (0)