Skip to content

Soundness Hole: Aliasing and Mutating static references in safe Rust #50162

Closed
@CryZe

Description

@CryZe
fn static_val() -> &'static mut u8 {
    let ref mut x = 0;
    x
}

fn main() {
    let ref1 = static_val();
    let ref2 = static_val();
    *ref1 = 20;
    *ref2 = 32;
    println!("{}", ref1);
}

The variable x is a mutable reference to a u8 that is promoted to a &'static mut u8. By calling that function multiple times, you can alias the mutable reference in safe Rust.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions