about summary refs log tree commit diff
path: root/src/tools/miri/tests/fail/dangling_pointers/deref_dangling_box.rs
blob: b22c1b4c5e81a7c1d0f9020fe12a07b44219cdc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Should be caught even without retagging
//@compile-flags: -Zmiri-disable-stacked-borrows
use std::ptr::{self, addr_of_mut};

// Deref'ing a dangling raw pointer is fine, but for a dangling box it is not.
// We do this behind a pointer indirection to potentially fool validity checking.
// (This test relies on the `deref_copy` pass that lowers `**ptr` to materialize the intermediate pointer.)

fn main() {
    let mut inner = ptr::without_provenance::<i32>(24);
    let outer = addr_of_mut!(inner).cast::<Box<i32>>();
    // Now `outer` is a pointer to a dangling reference.
    // Deref'ing that should be UB.
    let _val = unsafe { addr_of_mut!(**outer) }; //~ERROR: dangling box
}