From f58292448d138ceba671dc22916ba7e457939a55 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Wed, 13 Nov 2024 16:23:18 +0700 Subject: [PATCH] Add comments (cherry picked from commit ca5f3d35a5543ed5ee83d8574dc5340c59f0413d) --- libwasmvm/src/memory.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libwasmvm/src/memory.rs b/libwasmvm/src/memory.rs index b3d0bad9..cc30c623 100644 --- a/libwasmvm/src/memory.rs +++ b/libwasmvm/src/memory.rs @@ -217,6 +217,10 @@ impl UnmanagedVector { Some(data) => { let (ptr, len, cap) = { if data.capacity() == 0 { + // we need to explicitly use a null pointer here, since `as_mut_ptr` + // always returns a dangling pointer (e.g. 0x01) on an empty Vec, + // which trips up Go's pointer checks. + // This is safe because the Vec has not allocated, so no memory is leaked. (std::ptr::null_mut::(), 0, 0) } else { // Can be replaced with Vec::into_raw_parts when stable @@ -265,6 +269,10 @@ impl UnmanagedVector { if self.is_none { None } else if self.cap == 0 { + // capacity 0 means the vector was never allocated and + // the ptr field does not point to an actual byte buffer + // (we normalize to `null` in `UnmanagedVector::new`), + // so no memory is leaked by ignoring the ptr field here. Some(Vec::new()) } else { Some(unsafe { Vec::from_raw_parts(self.ptr, self.len, self.cap) })