-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Pointer-to-pointer + casts in FFI leads to hard-to-debug mysterious problems #17417
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
Triage: i've created a repository to assist anyone who wants to write this lint: https://github.com/steveklabnik/rust-issue-17417 |
Since new lints have a big impact on users of rustc, the policy is that they should go through the RFC process like other user-facing changes. As such, I'm going to give this one a close, but if anyone comes across this ticket and wants this lint, consider adding it to clippy and/or writing up an RFC. Thanks! |
@huonw, thank you for sharing a recipe for working with pointer-to-a-pointer APIs. It apparently should be documented in FFI chapter of the Rust book! |
Yes, that formula really does belong in the FFI section of the book. Saved me a million years of experimentation. |
Just ran into this today, it really should be documented in the book! |
is there some update? |
refactor: Prefer plain trait definitions over macros for impl_intern_value_trivial `impl_intern_value_trivial` can be defined with a trait directly, so prefer that over a macro definition.
If you have a C API taking a pointer-to-a-pointer, like
void**
, where the function fills in a pointer value, it's very easy to screw up if there is any pointer casting involved:This will always print
new pointer is 0x0
, no matter whatc_func
does.Reason: the
x as *mut ...
cast is creating a temporary, that's disconnected from the originalx
and thus the modification happens to the anonymous stack slot that stores the result of the cast. The code should be written something like(&mut x) as *mut _ as *mut *mut libc::c_void
.This is really subtle to debug, so we could have a lint that assists in this case: "did you mean to take a reference to the result of a cast in this FFI call" (could have it apply to non-FFI things too, and presumably it should only apply when there are
&mut
pointers involved).The text was updated successfully, but these errors were encountered: