rust/tests/compile-fail/null_pointer_write_zst.rs

11 lines
448 B
Rust
Raw Normal View History

2021-03-25 12:44:30 +01:00
// Some optimizations remove ZST accesses, thus masking this UB.
// compile-flags: -Zmir-opt-level=0
2021-04-15 10:00:39 +02:00
#[allow(deref_nullptr)]
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];
2020-05-01 14:43:59 +02:00
unsafe { *std::ptr::null_mut() = zst_val }; //~ ERROR memory access failed: 0x0 is not a valid pointer
}