-
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
Rust compiles invalid program which segfaults - borrowed pointer to trait object outlives original value #11374
Labels
A-type-system
Area: Type system
E-needs-test
Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Comments
Sounds similar to #5723; potential duplicate? |
This appears to be fixed:
Original example code updated to current rust:
|
Awesome! flagging as needstest (not sure if one already exists) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-type-system
Area: Type system
E-needs-test
Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Here is a very simple program which employs trait objects:
c.rc
:m1.rs
:m2.rs
:When this program is compiled and run, it fails with segmentation fault:
This program is not correct. You can see that
m1::for_stdin()
function produces aContainer<'a, ...>
for arbitrary lifetime'a
, but the local variabler
is valid only forfor_stdin()
body. A pointer tor
, however, is saved to theContainer
, and when it is accessed insidem2::main()
, a segmentation fault occurs. However, Rust still compiles it. I thought that borrowing checker should prevent this kind of errors.BTW, I can write
&mut r
or&mut r as &mut Reader
and it will compile either way, but I thought that when going from plain pointers to trait objects I should always specify exact kind of trait object. Not sure that I'm right here thought.The text was updated successfully, but these errors were encountered: