Closed
Description
if ptr is null, then length and capacity should be 0
http://doc.rust-lang.org/nightly/collections/vec/struct.Vec.html#method.from_raw_parts
Metadata
Metadata
Assignees
Labels
No labels
if ptr is null, then length and capacity should be 0
http://doc.rust-lang.org/nightly/collections/vec/struct.Vec.html#method.from_raw_parts
Activity
[-]Vec::from_raw_parts causes UB if ptr is null[/-][+]Vec::from_raw_parts incorrectly allow the pointer to be null[/+]thestinger commentedon Nov 24, 2014
The documentation is misleading. The pointer is not allowed to be null.
[-]Vec::from_raw_parts incorrectly allow the pointer to be null[/-][+]Vec::from_raw_parts docs incorrectly suggest the pointer is legal to be null[/+]mitsuhiko commentedon Nov 24, 2014
How do you make an empty slice if the pointer is not allowed to be null?
thestinger commentedon Nov 24, 2014
@mitsuhiko: The underlying representation is an implementation detail and shouldn't be documented beyond the ability to convert the raw parts obtained from a vector back into a vector.
thestinger commentedon Nov 24, 2014
The language and library documentation has a love affair with making far too many promises about the implementation. In the vector module, there are numerous errors when it comes to information about the vector's capacity too. It tends to guarantee that the capacity is exactly what was asked for rather than at least that much. It is allowed to set the capacity to a value provided by the allocator.
mitsuhiko commentedon Nov 24, 2014
Does that mean you are required to special case the empty case and then use a different way to make an empty vector?
thestinger commentedon Nov 24, 2014
No, it means you can't use this method outside of the standard library beyond converting from an existing vector.
thestinger commentedon Nov 24, 2014
If what you're asking is how it does this internally, the answer is that empty vectors along with zero-size allocations in general are allowed to be entirely arbitrary pointers. They will never be dereferenced and the compiler / library code will never attempt to deallocate them.
thestinger commentedon Nov 24, 2014
The documentation is not supposed to cover these implementation details. It would make sense to cover it in comments (which it is) or internal design documentation.
mitsuhiko commentedon Nov 24, 2014
In that case I suppose the docs should say: can only be constructed from components of an already existing vector.
Update documentation for from_raw_parts
auto merge of #19306 : steveklabnik/rust/gh19269, r=nikomatsakis,brson
2 remaining items