Skip to content

Use after free with mutable owned pointers #11192

Closed
@evanw

Description

@evanw

This code compiles and runs but seems to allow a use-after-free:

struct Foo {
  x: int
}

impl Drop for Foo {
  fn drop(&mut self) {
    println!("drop {}", self.x);
  }
}

fn main() {
  let mut ptr = ~Foo { x: 0 };
  let test = |foo: &Foo| {
    println!("access {}", foo.x);
    ptr = ~Foo { x: ptr.x + 1 };
    println!("access {}", foo.x);
  };
  test(ptr);
}

The output is:

access 0
drop 0
access 0
drop 1

This is with rustc 0.8. It may be related to #9853 but I'm not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions