From 7b357ba84dabdcd424c0c57c3328239c620bc47e Mon Sep 17 00:00:00 2001 From: Emilie Burgun Date: Sun, 29 Dec 2024 22:04:40 +0100 Subject: [PATCH] Fix Heap::drop not accounting for null-initialized HeapInner --- src/machine/heap.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/machine/heap.rs b/src/machine/heap.rs index 5dd33a9b..61614dab 100644 --- a/src/machine/heap.rs +++ b/src/machine/heap.rs @@ -24,17 +24,29 @@ pub struct Heap { impl Drop for Heap { fn drop(&mut self) { - unsafe { - let layout = alloc::Layout::array::(self.inner.byte_cap).unwrap(); - alloc::dealloc(self.inner.ptr, layout); + if !self.inner.ptr.is_null() { + unsafe { + let layout = alloc::Layout::array::(self.inner.byte_cap).unwrap(); + alloc::dealloc(self.inner.ptr, layout); + } } } } +// TODO: verify the soundness of the various accesses to `ptr`, +// or rely on a Vec-like library with fallible allocations. #[derive(Debug)] struct InnerHeap { ptr: *mut u8, + + /// # Safety + /// + /// Must be equal to zero when `ptr.is_null()`. byte_len: usize, + + /// # Safety + /// + /// Must be equal to zero when `ptr.is_null()`. byte_cap: usize, } -- 2.54.0