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

borrowck unsoundness #13497

Closed
emberian opened this issue Apr 13, 2014 · 10 comments · Fixed by #17721
Closed

borrowck unsoundness #13497

emberian opened this issue Apr 13, 2014 · 10 comments · Fixed by #17721
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

@emberian
Copy link
Member

fn read_lines_borrowed() -> ~[&str] {
    let rawLines: ~[~str] = ~[~"foo  ", ~"  bar"];
    rawLines.iter().map(|l| l.trim()).collect()
}

fn main() {
    println!("{}", read_lines_borrowed());
}

read_lines_borrowed is not rejected. but the value it returns cannot possibly be valid, since rawLines is going to be free'd after it returns, leaving the collected references dangling.

@emberian
Copy link
Member Author

@emberian
Copy link
Member Author

(specifically the only valid lifetime should be 'static)

@emberian
Copy link
Member Author

A longer example, which this is extracted from, exhibits memory unsafety: https://gist.github.com/anonymous/10601934

@alexcrichton
Copy link
Member

This is likely a dupe of #12223, but I'm not familiar enough with the code to say for certain.

@emberian
Copy link
Member Author

They do appear similar.

@flaper87
Copy link
Contributor

I agree with @alexcrichton I think this is a dupe of #13497

mind if I copy this example in that bug and close this one? The other bug has already been triaged and prioritized.

@emberian
Copy link
Member Author

I'm not convinced this is actually a dupe, the other seems to have to do
with dropping a value that ought not to be, whereas this one is a lifetime
mismatch (doesn't live long enough). Not sure.

On Mon, Apr 14, 2014 at 5:00 PM, Flavio Percoco Premoli <
notifications@github.com> wrote:

I agree with @alexcrichton https://github.com/alexcrichton I think this
is a dupe of #13497 #13497

mind if I copy this example in that bug and close this one? The other bug
has already been triaged and prioritized.


Reply to this email directly or view it on GitHubhttps://github.com//issues/13497#issuecomment-40417480
.

http://octayn.net/

@nikomatsakis
Copy link
Contributor

I'll try to investigate today.

@jfager
Copy link
Contributor

jfager commented Jul 26, 2014

The current-rust translations of this seems to be correctly rejected:

With:

fn read_lines_borrowed() -> Vec<&str> {
    let rawLines: Vec<String> = vec!["foo  ".to_string(), "  bar".to_string()];
    rawLines.iter().map(|l| l.as_slice().trim()).collect()
}

I get:

$ rustc r13497.rs 
r13497.rs:1:33: 1:37 error: missing lifetime specifier [E0106]
r13497.rs:1 fn read_lines_borrowed() -> Vec<&str> {

And with:

fn read_lines_borrowed<'a>() -> Vec<&'a str> {
    let rawLines: Vec<String> = vec!["foo  ".to_string(), "  bar".to_string()];
    rawLines.iter().map(|l| l.as_slice().trim()).collect()
}

I get:

$ rustc r13497.rs 
r13497.rs:3:5: 3:13 error: `rawLines` does not live long enough
r13497.rs:3     rawLines.iter().map(|l| l.as_slice().trim()).collect()
                ^~~~~~~~
r13497.rs:1:46: 4:2 note: reference must be valid for the lifetime 'a as defined on the block at 1:45...
r13497.rs:1 fn read_lines_borrowed<'a>() -> Vec<&'a str> {
r13497.rs:2     let rawLines: Vec<String> = vec!["foo  ".to_string(), "  bar".to_string()];
r13497.rs:3     rawLines.iter().map(|l| l.as_slice().trim()).collect()
r13497.rs:4 }
r13497.rs:1:46: 4:2 note: ...but borrowed value is only valid for the block at 1:45
r13497.rs:1 fn read_lines_borrowed<'a>() -> Vec<&'a str> {
r13497.rs:2     let rawLines: Vec<String> = vec!["foo  ".to_string(), "  bar".to_string()];
r13497.rs:3     rawLines.iter().map(|l| l.as_slice().trim()).collect()
r13497.rs:4 }
error: aborting due to previous error

@alexcrichton
Copy link
Member

Thanks @jfager! Flagging as needstest.

flip1995 pushed a commit to flip1995/rust that referenced this issue Oct 18, 2024
…rednet

unused_result_ok: added in Clippy 1.82.0, not 1.70.0

changelog: none

Fix rust-lang#13497
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants