-
Notifications
You must be signed in to change notification settings - Fork 529
UB: Dangling Pointer, ZSTs. #1433
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
Interpretation A, of course.
A pointer is allows to point one past the end of an existing allocation, as long as the memory is never accessed. See e.g. the docs for
Yes, they are. In particular, if you cast an integer to a pointer to ZST it is always valid, but if it was part of some allocation and this allocation was freed it is invalid.
It is not dangling, as it is constructed from an integer literal for zero sized type, which is valid.
I'm not sure if this is guaranteed, but the common understanding is that not. Uninitialized is a property of the bytes, and if there are no bytes there is no uninitialized. The type does not have a restricted set of values, it has one value and that's it. |
@ChayimFriedman2, thanks for the replies! Overall would be nice to see all that in the text of the book as a confirmation (hence this bug). @ChayimFriedman2, I have some extra questions. 5.
Yes, a pointer is allowed to point one past the end of an existing allocation, that totally makes sense to me (the
This has consequence for what causes the undefined behavior from dangling pointers (see occurrences of "dangling" there), and what does not. In particular
@ChayimFriedman2, do you agree with such a conclusion? 6.
(I assume, you mean "it is constructed from a non-zero integer literal")
|
Of course dereferencing such pointer is UB: it is out-of-bounds for the allocation.
Because
"Dangling" has multiple meanings; in this context, it means aligned but not points to an existing allocation. For ZSTs, this is enough. |
FWIW the rules for zero-sized accesses will become a lot simpler soon: rust-lang/rust#117329. And yes "dangling" is unfortunately an overloaded term. Also, some of your questions are entirely unrelated with each other; it's better to open separate issues for them as otherwise the discussion gets all mixed up.
It is intended as a clarifying remark.
Note that this has since been changed; that text no longer occurs in the reference.
Depends on the ZST. For
When #117329 lands, the rules will be always the same, both for zero-sized types and for non-zero-sized types: take the set of memory locations that the pointer points to (it's a set, not a single location! This entire "past the end" business is completely unnecessary in Rust. |
"16.2. Behavior considered undefined" / "Dangling pointers".
The definition of dangling pointer confuses.
1.
Which is the right interpretation of the paragraph above?
Interpretation A:
If the size is 0, then in order for the pointer to NOT be dangling the pointer must either point inside of a live allocation (including pointing just after the last byte of the allocation), or it must be directly constructed from a non-zero integer literal.
Interpretation B
{ If the size is 0, then even the dangling pointer must either point inside of a live allocation (including pointing just after the last byte of the allocation), or it must be directly constructed from a non-zero integer literal.
Otherwise, <WHAT?> }
2.
A. What is the purpose of the fragment "(so in particular they all have to be part of some allocation)"? What will be lost if that fragment is removed?
B. If the pointer points to bytes immediately after the allocation, is that pointer dangling?
3.
Also, (on the same page) in Behavior considered undefined
A. I assume that this paragraph is still applicable to the zero-sized types. In other words, dereferencing a dangling or unaligned zero-sized type raw pointer, is also an undefined behavior. Correct?
B. In the Rustonomicon, 9.11. Final Code, the fragment below (the comments are mine) dereferences the dangling zero-sized type pointer (which is undefined behavior). Correct?
4.
My interpretation: Reading from uninitialized memory, even by using the correctly aligned zero-sized type pointer pointing to a correct allocation, is undefined behavior.
Is this interpretation correct?
Would be nice to see in the book the clarification of those.
The text was updated successfully, but these errors were encountered: