Skip to content

Memory unsafety using & bindings more than one level deep when destructuring #19997

Closed
@pythonesque

Description

@pythonesque

This segfaults.

// Note: you can also replicate this with this function signature.
// fn foo<'a, 'b, 'c>(mut pair: (&'c mut &'a mut Vec<Option<String>>, &'b mut Vec<&'a mut Option<String>>)) {
//     let mut pair = (&mut a, &mut b);
// }

fn main() {
    let mut a = Vec::new();
    let mut a = &mut a;
    let mut b = Vec::new();
    for _ in range(0u8, 9) {
        //foo((&mut a, &mut b));
        // Correctly complains if you do: let (ref mut a, ref mut b) = (&mut a, &mut b);
        // Also correctly complains in Rust 0.12.
        let (&ref mut a, ref mut b) = (&mut a, &mut b);
        a.push(Some("".to_string()));
        let index = a.len() - 1;
        let data = a.as_mut_slice().get_mut(index).unwrap();
        b.push(data);
    }
    println!("{}", b);
}

Metadata

Metadata

Assignees

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