allocator-api2 icon indicating copy to clipboard operation
allocator-api2 copied to clipboard

`Box::as(_mut)_ptr` is unsound, `alloc` feature does not support msrv

Open bluurryy opened this issue 9 months ago • 0 comments

The current implementation of as_mut_ptr() looks like this

// This is a primitive deref, not going through `DerefMut`, and therefore not materializing
// any references.
&raw mut **b

What the comment says is true for the standard library type, but allocator_api2's Box has no primitive deref and it will go through DerefMut.

This causes a stacked borrows violation when running as_ptr's doc test in miri.

The implementation can be replaced by

b.0.as_ptr()

This has the side effect that the alloc feature becomes compatible with the msrv.


I adapted Box's doc tests so they actually test allocator_api2::boxed::Box and they all pass miri[^1] with that change. Would you be interested in a PR that makes those changes to the doc tests?

[^1]: With MIRIFLAGS=-Zmiri-ignore-leaks, because some tests intentionally leak

bluurryy avatar May 09 '25 18:05 bluurryy