]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/dangling_pointers/storage_dead_dangling.rs
Auto merge of #104572 - pkubaj:patch-1, r=cuviper
[rust.git] / src / tools / miri / tests / fail / dangling_pointers / storage_dead_dangling.rs
1 // This should fail even without validation, but some MIR opts mask the error
2 //@compile-flags: -Zmiri-disable-validation -Zmir-opt-level=0 -Zmiri-permissive-provenance
3
4 static mut LEAK: usize = 0;
5
6 fn fill(v: &mut i32) {
7     unsafe {
8         LEAK = v as *mut _ as usize;
9     }
10 }
11
12 fn evil() {
13     unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR: is a dangling pointer
14 }
15
16 fn main() {
17     let _y;
18     {
19         let mut x = 0i32;
20         fill(&mut x);
21         _y = x;
22     }
23     // Now we use a pointer to `x` which is no longer in scope, and thus dead (even though the
24     // `main` stack frame still exists). We even try going through a `usize` for extra sneakiness!
25     evil();
26 }