Skip to content

Commit b00578d

Browse files
adri326mthom
authored andcommitted
Fix Heap::drop not accounting for null-initialized HeapInner
1 parent dbc1157 commit b00578d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/machine/heap.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,29 @@ pub struct Heap {
2424

2525
impl Drop for Heap {
2626
fn drop(&mut self) {
27-
unsafe {
28-
let layout = alloc::Layout::array::<u8>(self.inner.byte_cap).unwrap();
29-
alloc::dealloc(self.inner.ptr, layout);
27+
if !self.inner.ptr.is_null() {
28+
unsafe {
29+
let layout = alloc::Layout::array::<u8>(self.inner.byte_cap).unwrap();
30+
alloc::dealloc(self.inner.ptr, layout);
31+
}
3032
}
3133
}
3234
}
3335

36+
// TODO: verify the soundness of the various accesses to `ptr`,
37+
// or rely on a Vec-like library with fallible allocations.
3438
#[derive(Debug)]
3539
struct InnerHeap {
3640
ptr: *mut u8,
41+
42+
/// # Safety
43+
///
44+
/// Must be equal to zero when `ptr.is_null()`.
3745
byte_len: usize,
46+
47+
/// # Safety
48+
///
49+
/// Must be equal to zero when `ptr.is_null()`.
3850
byte_cap: usize,
3951
}
4052

0 commit comments

Comments
 (0)