Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec::from_raw_parts docs incorrectly suggest the pointer is legal to be null #19269

Closed
mahkoh opened this issue Nov 24, 2014 · 9 comments · Fixed by #19306
Closed

Vec::from_raw_parts docs incorrectly suggest the pointer is legal to be null #19269

mahkoh opened this issue Nov 24, 2014 · 9 comments · Fixed by #19306

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Nov 24, 2014

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

@huonw huonw added the A-docs label Nov 24, 2014
@huonw huonw changed the title Vec::from_raw_parts causes UB if ptr is null Vec::from_raw_parts incorrectly allow the pointer to be null Nov 24, 2014
@thestinger
Copy link
Contributor

The documentation is misleading. The pointer is not allowed to be null.

@huonw huonw changed the title 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 Nov 24, 2014
@mitsuhiko
Copy link
Contributor

How do you make an empty slice if the pointer is not allowed to be null?

@thestinger
Copy link
Contributor

@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
Copy link
Contributor

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
Copy link
Contributor

Does that mean you are required to special case the empty case and then use a different way to make an empty vector?

@thestinger
Copy link
Contributor

No, it means you can't use this method outside of the standard library beyond converting from an existing vector.

@thestinger
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

In that case I suppose the docs should say: can only be constructed from components of an already existing vector.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Nov 25, 2014
lnicola added a commit to lnicola/rust that referenced this issue Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants