Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
(cherry picked from commit ca5f3d3)
  • Loading branch information
chipshort authored and mergify[bot] committed Nov 13, 2024
1 parent a641c50 commit f582924
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 @@ -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::<u8>(), 0, 0)
} else {
// Can be replaced with Vec::into_raw_parts when stable
Expand Down Expand Up @@ -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) })
Expand Down

0 comments on commit f582924

Please sign in to comment.