rust/tests/fail/dangling_pointers/maybe_null_pointer_write_zst.rs

12 lines
523 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() {
// This pointer *could* be NULL so we cannot load from it, not even at ZST.
// 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];
let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *mut [u8; 0];
2021-07-15 13:33:08 -05:00
unsafe { *ptr = zst_val; } //~ ERROR out-of-bounds
}