Skip to content

rustc: references can escape their lifetime via trait objects #14868

Closed
@aturon

Description

@aturon

A trait impl on a reference can be used to violate lifetimes via trait objects. The following segfaults on my OSX machine:

trait Oops {
    fn expose<'b>(&'b self) -> &'b Vec<u8>;
}

impl<'a> Oops for &'a Vec<u8> {
    fn expose<'b>(&'b self) -> &'b Vec<u8> {
        *self
    }
}

fn hide(x: &Vec<u8>) -> Box<Oops> {
    box x as Box<Oops>
}

fn hidden_pointer_into_frame() -> Box<Oops> {
    let v = vec!(37);
    hide(&v)
}

fn main() {
    let hp: Box<Oops> = hidden_pointer_into_frame();
    let p: &Vec<u8> = hp.expose();
    println!("{}", *p)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions