-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Vec::as_ptr for empty vectors is undocumented #39625
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
Comments
Yes, we should document this for sure. |
FYI, this is precisely why rust uses uses 0x1 for zero-sized things instead of 0x0 (null); it does this to distinguish between "nothing" and "something empty/zero sized". |
Would it be accurate to say that it returning |
Yes, I would say something like "the pointer value of an empty vector is meaningless", but uh, worded better. Not after midnight 😉 |
Not exactly. It *can't* be 0 (null pointer optimization) and setting it to 1 ensures that most accidental dereferences will result in a segfault (most operating systems avoid mapping memory into the first few pages of address space).
|
This not only happen for Vec, but for other things of 0 size allocated on the heap as well, (namely Box<()>). While you might say it's pointless to do Box::new(()), it can still happen and it should be documented. I suspect it works the same way for Rc<()> and other things like that, so a chapter in the nomicon would be great I think. At the very least, say something in Box::into_raw_ptr() about 0 size structs, and add a link from Vec::as_ptr() to this part. |
Opened a pull request here: #39757. In the pull request, I used the term 'meaningless' @steveklabnik mentioned above. I don't think it's a great term, but I can't think of anything better right now. Please please share better ideas! |
There are multiple aspects to this issue:
In that sense the only real change that’s possible in documentation is
|
docs team triage: p-medium, still need to sort out the exact details, thanks for the above comment @nagisa |
I am going to send in a PR for this based on @nagisa 's wording. |
I have completely turned 180 on this. There are too many pitfalls, as @nagisa says. I don't think that sentence really adds enough to actually understand what's going on here. Sadly, I think this is the best we can do. Giving this a close now. |
Vec::as_ptr
returns a pointer to address0x1
for empty vectors. As it seems, this is an internal implementation detail forRawVec
and allocation.This behavior is unexpected and not documented. In certain scenarios it might cause a segmentation fault (e.g. due to failed checks for a null pointer when dealing with FFI).
The text was updated successfully, but these errors were encountered: