-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Fix doc for pointer's validity in zst operation #134912
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Noratrieb (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the wrong way around, null is valid for zero-sized accesses. See Miri's output for this code (and ignore the lint).
You should fix the ptr::read docs instead
cc @RalfJung maybe you have opinions on how to best structure it
This has already been fixed as far as I can tell -- the docs at https://doc.rust-lang.org/nightly/std/ptr/fn.read.html do not contain the sentence you quote.
"dangling" is used in different ways in different places, I don't think there is a standard definition. That's why we make our definition explicit. I don't see a reason to change the terminology here. Also your PR doesn't even change anything here. Why are your PR description and the PR contents so disconnected? |
As @RalfJung mentioned, the issue with read has been resolved in the nightly version. However, several other APIs still require a valid non-null pointer for ZSTs, such as write_unaligned, read_unaligned, from_raw_parts,from_raw_parts_mut, and drop_in_place. |
There are many different notions of "valid". Nobody claims that null pointers are always valid. The claim is that they are valid for reads and writes of size 0. the _unaligned method docs should be fixed. The rest indeed does not allow null pointers. |
The doc of core::ptr states that |
Context matters. You are taking that sentence out of context.
Again, the question "is X valid" is meaningless. You have to ask "valid for what". The pointer |
@RalfJung Thank you for your responses. I am trying to understand why from_raw_parts and from_raw_parts_mut require non-null pointers for ZSTs. Additionally, I’m exploring the fundamental rules for determining whether a null pointer to a ZST type can be valid in a given context. Is this requirement because |
☔ The latest upstream changes (presumably #134952) made this pull request unmergeable. Please resolve the merge conflicts. |
Yes. |
Fix doc for read&write unaligned in zst operation ### PR Description This PR addresses an inconsistency in the Rust documentation regarding `read_unaligned ` and `write_unaligned` on zero-sized types (ZSTs). The current documentation for [pointer validity](https://doc.rust-lang.org/nightly/std/ptr/index.html#safety) states that for zero-sized types (ZSTs), null pointers are valid: > For zero-sized types (ZSTs), every pointer is valid, including the null pointer. However, there is an inconsistency in the documentation for the unaligned read operation in the function [ptr::read_unaligned](https://doc.rust-lang.org/nightly/std/ptr/fn.read_unaligned.html)(as well as `write_unaligned`), which states: > Note that even if T has size 0, the pointer must be non-null. This change is also supported by [PR rust-lang#134912](rust-lang#134912) > the _unaligned method docs should be fixed.
Rollup merge of rust-lang#134953 - DiuDiu777:unaligned-doc, r=RalfJung Fix doc for read&write unaligned in zst operation ### PR Description This PR addresses an inconsistency in the Rust documentation regarding `read_unaligned ` and `write_unaligned` on zero-sized types (ZSTs). The current documentation for [pointer validity](https://doc.rust-lang.org/nightly/std/ptr/index.html#safety) states that for zero-sized types (ZSTs), null pointers are valid: > For zero-sized types (ZSTs), every pointer is valid, including the null pointer. However, there is an inconsistency in the documentation for the unaligned read operation in the function [ptr::read_unaligned](https://doc.rust-lang.org/nightly/std/ptr/fn.read_unaligned.html)(as well as `write_unaligned`), which states: > Note that even if T has size 0, the pointer must be non-null. This change is also supported by [PR rust-lang#134912](rust-lang#134912) > the _unaligned method docs should be fixed.
Thanks, @RalfJung. I noticed that the document mentions the requirement of non-null for pointer-to-reference conversion. I believe this might lead to confusion regarding whether null pointers for ZSTs are excluded. Inspired by related issues, we started a project which aims to clarify all safety properties by defining them with primitive safety properties. We also plan to label unsafe APIs based on these "formally defined" properties. Our initial proposal for primitive safety properties can be found here: link. Do you have any comments or suggestions? |
Pointers have to be non-null when converted to references. I don't see the potential for confusion here. You have to understand that different operations have different preconditions; that's why each operation documents its particular preconditions.
Sorry, I won't have the time to review such an extensive document. Regarding this PR, #134930 tweaked the wording a bit to clarify. I don't think any further changes are needed here. The PR as-is is definitely wrong, so I will close it. If you think the latest docs at https://doc.rust-lang.org/nightly/std/ptr/index.html are still unclear, please file an issue or a new PR explaining why and potentially suggesting concrete improvements. |
PR Description
This pr addresses a discrepancy in the documentation regarding pointer validity for zero-sized types (ZSTs).
According to the documentation on core::ptr, it states that
However, this is contradicted by the documentation on core::ptr::read, which specifies that
This creates a contradiction, as
core::ptr::read
explicitly states that pointers to ZSTs must be non-null, while thecore::ptr
documentation implies that null pointers are valid for ZSTs.Suggestion
Besides, I would like to suggest a revision to the following sentence in the core::ptr documentation:
The term "dangling" traditionally refers to a pointer that still exists but points to memory that has been freed or is otherwise invalid. In contrast, a null pointer is not considered a "dangling" pointer, but rather an invalid pointer by definition.
So in this context, I think replacing "dangling" with "invalid" would be better?