]> git.lizzy.rs Git - rust.git/blob - tests/fail/dangling_pointers/maybe_null_pointer_write_zst.rs
7f50b9827d94b5fd224915b14ec32067670ac069
[rust.git] / tests / fail / dangling_pointers / maybe_null_pointer_write_zst.rs
1 // Some optimizations remove ZST accesses, thus masking this UB.
2 // compile-flags: -Zmir-opt-level=0
3
4 fn main() {
5     // This pointer *could* be NULL so we cannot load from it, not even at ZST.
6     // Not using the () type here, as writes of that type do not even have MIR generated.
7     // Also not assigning directly as that's array initialization, not assignment.
8     let zst_val = [1u8; 0];
9     let ptr = (&0u8 as *const u8).wrapping_sub(0x800) as *mut [u8; 0];
10     unsafe { *ptr = zst_val; } //~ ERROR out-of-bounds
11 }