Vec::from_raw_parts
docs do not correctly handle empty buffers
#119304
Labels
A-collections
Area: `std::collection`
A-docs
Area: documentation for any part of the project, including the compiler, standard library, and tools
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Location
https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts
Summary
The docs state the precondition "
ptr
must have been allocated using the global allocator, such as via the alloc::alloc function", which means that the following code is unsound sinceVec::new
does not allocate to produce the pointer:I believe this to be highly surprising, and probably unintended.
Vec::from_raw_parts
should allowptr
not to be obtained from an allocation ifcapacity
times the size ofT
is zero.If the documentation is correct
InPlaceDstBufDrop
is unsound: https://github.com/rust-lang/rust/blob/master/library/alloc/src/vec/in_place_drop.rs#L37String::from_raw_parts
andVec::from_raw_parts_in
have the same issue.The text was updated successfully, but these errors were encountered: