-
Couldn't load subscription status.
- Fork 13.9k
Open
Labels
A-collectionsArea: `std::collections`Area: `std::collections`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
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 since Vec::new does not allocate to produce the pointer:
fn reassemble<T>(mut v: Vec<T>) -> Vec<T> { let capacity = v.capacity(); let ptr = v.as_mut_ptr(); let length = v.len(); std::mem::forget(v); unsafe {Vec::from_raw_parts(ptr, length, capacity)} }I believe this to be highly surprising, and probably unintended.
Vec::from_raw_parts should allow ptr not to be obtained from an allocation if capacity times the size of T 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#L37
String::from_raw_parts and Vec::from_raw_parts_in have the same issue.
daira, JarvisCraft and wyfo
Metadata
Metadata
Assignees
Labels
A-collectionsArea: `std::collections`Area: `std::collections`A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.