Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Nov 13, 2024
1 parent 4c0d2ea commit ca5f3d3
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libwasmvm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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::<u8>(), 0, 0)
} else {
// Can be replaced with Vec::into_raw_parts when stable
Expand Down Expand Up @@ -266,6 +270,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) })
Expand Down

0 comments on commit ca5f3d3

Please sign in to comment.