File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,10 @@ impl UnmanagedVector {
218
218
Some ( data) => {
219
219
let ( ptr, len, cap) = {
220
220
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.
221
225
( std:: ptr:: null_mut :: < u8 > ( ) , 0 , 0 )
222
226
} else {
223
227
// Can be replaced with Vec::into_raw_parts when stable
@@ -266,6 +270,10 @@ impl UnmanagedVector {
266
270
if self . is_none {
267
271
None
268
272
} 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.
269
277
Some ( Vec :: new ( ) )
270
278
} else {
271
279
Some ( unsafe { Vec :: from_raw_parts ( self . ptr , self . len , self . cap ) } )
You can’t perform that action at this time.
0 commit comments