Skip to content

borrowck allows use-after-free with weird struct A{foo: Option<~fn(&mut A)>} construction #9853

Closed
@ben0x539

Description

@ben0x539

Here's a bit of a verbose test case for a situation where I get away with accessing a value in a ~-closure environment after its destructor has run. valgrind also complains. Not sure whether this falls under a known bug, I didn't really know what terms to search for.

struct A {
    foo : Option<~fn(&mut A)>
}

impl A {
    fn call(&mut self) {
        (*self.foo.get_ref())(self); // this is probably the broken bit
    }
}

struct S {
    msg: ~str
}

impl Drop for S {
    fn drop(&mut self) {
        println!("drop {:?}", *self);
        self.msg = ~"dropped";
    }
}

fn main() {
    let x = S { msg: ~"ok" };
    let f: ~fn(&mut A) = |a| { a.foo = None; println!("{:?}", x); }; // this frees closure environment via `a`, then accesses it
    let mut a = A { foo: Some(f) };
    a.call();
}

output:

drop S{msg: ~"ok"}
S{msg: ~"dropped"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions