Skip to content

Cell is unsound around variance #11385

Closed
@pcwalton

Description

@pcwalton

The following creates a dangling pointer to an outer stack frame:

use std::cell::Cell;

struct Foo<'a> {
    x: int,
    y: Cell<Option<&'a int>>,
}

fn f() -> Foo {
    Foo {
        x: 1,
        y: Cell::new(None),
    }
}

fn g<'a>(x: &'a Foo<'a>) {
    x.y.set(Some(&x.x));
}

fn h() -> Foo {
    let x = f();
    g(&x);
    x
}

fn main() {
    let x = h();
    // kaboom
}

This manifested as a rustc segfault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemI-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