rust/tests/compile-fail/zst2.rs

16 lines
626 B
Rust
Raw Normal View History

2021-03-25 06:44:30 -05:00
// Some optimizations remove ZST accesses, thus masking this UB.
// compile-flags: -Zmir-opt-level=0
fn main() {
// Not using the () type here, as writes of that type do not even have MIR generated.
// Also not assigning directly as that's array initialization, not assignment.
let zst_val = [1u8; 0];
// make sure ZST accesses are checked against being "truly" dangling pointers
// (into deallocated allocations).
let mut x_box = Box::new(1u8);
let x = &mut *x_box as *mut _ as *mut [u8; 0];
drop(x_box);
2020-03-08 17:34:54 -05:00
unsafe { *x = zst_val; } //~ ERROR dereferenced after this allocation got freed
}