From: Emilie Burgun Date: Sun, 29 Dec 2024 21:04:40 +0000 (+0100) Subject: Fix Heap::drop not accounting for null-initialized HeapInner X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=c3c7b3173dbf1dc1d9bba886d7a7b1f84e4e4176;p=scryer-prolog.git Fix Heap::drop not accounting for null-initialized HeapInner --- 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, }