-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Auto-coercing from &T to *T is unsafe #7694
Comments
I don't really see how it could be unsafe, because you can already create arbitrary dangling raw pointers from integers. Any interface taking a raw pointer and then dereferencing it at some point has to be marked The |
@thestinger: I've updated my gist to make the Yes, the unsafe pointer is unsafe, and you need to be in an unsafe block to dereference it or pass it to a C function, but I don't think that means we should turn off all the safety features of Rust when in an unsafe block. If we can prevent this dangling pointer being created, I feel we should even in unsafe blocks. I'd rather force people to use So my vote right now is that we:
edit: I submitted this early and incomplete by accident. |
I don't quite see the point of Also, I think that the current "rules" regarding the lifetime of temporaries are too short, which makes it easier to footgun in examples like this one. All temporaries ought to live at least as love as the current statement, I think, what's unclear to me is if we should possibly infer a longer lifetime. (#3511) |
@nikomatsakis: We still have a footgun even if we make temporaries live to the end of the current statement. For example, if we take my gist and set main to:
Even if that temporary lives as long as the statement we'll still have a dangling pointer because of the auto-coercion from a As far as I know, there are two main differences between a |
@erickt I agree it's not foolproof, but I don't consider that example to be the same level of footgun. Still, having a lifetime associated with unsafe pointers might have value. |
@erickt do you think this can be closed? |
Triage bump. @erickt status? |
It's not actually memory |
@thestinger is right in remembering why I thought this ticket was closed a long time ago. This unsafety just needs to be well documented. |
Retry on some download errors in lintcheck I'm currently on spotty wifi right now. It is shocking the number of things that break when you lose connection for a few seconds. Some 500 errors should probably also be retried, but this fixes my issue. changelog: None
I'm working on a
*libc::c_char
wrapper library, and I wanted to make sure that people didn't accidentally grab the inner pointer, deallocate theCStr
, then pass the pointer to a function. I thought this would be a great place to use regions, so instead of returning*libc::c_char
I returned&'self libc::c_char
. This works in all cases except for when we auto-coerce from a&T
to a*T
.You can find a demonstration of this problem in this gist. While
CStr
is full of unsafe code, I believe the interface is safe. The error is demonstrated in thebar
/baz
functions.bar
properly reports that the lifetime of the&'self libc::c_char
does not live long enough, butbaz
doesn't mention a problem at all.I can think of a couple options to fixing this:
&T
to a*T
. This at least adds a small roadblock force the end user to think about this cast, but it won't save you from shooting yourself in the foot if you so choose.cast::transmute()
to forget the region.&libc::c_char
instead of*libc::c_char
. This is a nice short term bandaid for the stdlib, but doesn't really help out end users writing C bindings that don't know about this behavior.The text was updated successfully, but these errors were encountered: