]> Repositorios git - scryer-prolog.git/commitdiff
Fix Heap::drop not accounting for null-initialized HeapInner
authorEmilie Burgun <[email protected]>
Sun, 29 Dec 2024 21:04:40 +0000 (22:04 +0100)
committerMark Thom <[email protected]>
Wed, 23 Apr 2025 06:32:32 +0000 (23:32 -0700)
src/machine/heap.rs

index 5dd33a9bfd07dadc61ca0bed7a6085724c0476d0..61614dab5542695832c16cae180281f536a4c390 100644 (file)
@@ -24,17 +24,29 @@ pub struct Heap {
 
 impl Drop for Heap {
     fn drop(&mut self) {
-        unsafe {
-            let layout = alloc::Layout::array::<u8>(self.inner.byte_cap).unwrap();
-            alloc::dealloc(self.inner.ptr, layout);
+        if !self.inner.ptr.is_null() {
+            unsafe {
+                let layout = alloc::Layout::array::<u8>(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,
 }