Skip to content

Commit ca5f3d3

Browse files
committed
Add comments
1 parent 4c0d2ea commit ca5f3d3

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

libwasmvm/src/memory.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ impl UnmanagedVector {
218218
Some(data) => {
219219
let (ptr, len, cap) = {
220220
if data.capacity() == 0 {
221+
// we need to explicitly use a null pointer here, since `as_mut_ptr`
222+
// always returns a dangling pointer (e.g. 0x01) on an empty Vec,
223+
// which trips up Go's pointer checks.
224+
// This is safe because the Vec has not allocated, so no memory is leaked.
221225
(std::ptr::null_mut::<u8>(), 0, 0)
222226
} else {
223227
// Can be replaced with Vec::into_raw_parts when stable
@@ -266,6 +270,10 @@ impl UnmanagedVector {
266270
if self.is_none {
267271
None
268272
} else if self.cap == 0 {
273+
// capacity 0 means the vector was never allocated and
274+
// the ptr field does not point to an actual byte buffer
275+
// (we normalize to `null` in `UnmanagedVector::new`),
276+
// so no memory is leaked by ignoring the ptr field here.
269277
Some(Vec::new())
270278
} else {
271279
Some(unsafe { Vec::from_raw_parts(self.ptr, self.len, self.cap) })

0 commit comments

Comments
 (0)