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

Documentation for addr_of_mut! is contradictory #94473

Closed
nicholasbishop opened this issue Mar 1, 2022 · 4 comments
Closed

Documentation for addr_of_mut! is contradictory #94473

nicholasbishop opened this issue Mar 1, 2022 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@nicholasbishop
Copy link
Contributor

The documentation for addr_of_mut! says:

Note, however, that the expr in addr_of_mut!(expr) is still subject to all the usual rules. In particular, addr_of_mut!(*ptr::null_mut()) is Undefined Behavior because it dereferences a null pointer.

However, in the example for Creating a pointer to uninitialized data it shows this code:

let f1_ptr = unsafe { ptr::addr_of_mut!((*uninit.as_mut_ptr()).field) };

In that example it appears as though expr contains a dereference of uninitialized data, which is not normally valid. That seems in conflict with the earlier warning that "all the usual rules" apply.

I'm guessing that maybe dereferencing is allowed in expr as long as the pointer isn't dangling?

@nicholasbishop nicholasbishop added the C-bug Category: This is a bug. label Mar 1, 2022
@RalfJung
Copy link
Member

RalfJung commented Mar 5, 2022

In that example it appears as though expr contains a dereference of uninitialized data, which is not normally valid.

Why do you think that is the case?
"Dereferencing" (as in, creating a place from) a pointer to uninitialized memory is fine. Only actually reading from that place is a problem (it depends on the type, some types allow uninit memory -- like MaybeUninit<u64> -- and others do not).

@nicholasbishop
Copy link
Contributor Author

Thanks, I didn't realize that was allowed. Getting a good handle on what is and isn't allowed with pointers in Rust is not always super easy :)

Is there an existing piece of documentation that spells out what all the rules are? It would be great to hyperlink all the usual rules to that documentation, or just directly list out what the rules are inline if they are sufficiently short.

@RalfJung
Copy link
Member

RalfJung commented Mar 6, 2022

Yeah, it's all kind of complicated. ;)

Basically the core rules are these ones.

nicholasbishop added a commit to nicholasbishop/rust that referenced this issue Mar 20, 2022
Explicitly list the things you aren't allowed to do with `expr`, and add
a link to the exact definition of a dangling pointer.

Fixes rust-lang#94473
@nicholasbishop
Copy link
Contributor Author

Closing this. I think #114902 covers it pretty well :)

@nicholasbishop nicholasbishop closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants