2021-03-25 06:44:30 -05:00
|
|
|
// Some optimizations remove ZST accesses, thus masking this UB.
|
2022-07-08 11:08:32 -05:00
|
|
|
//@compile-flags: -Zmir-opt-level=0
|
2021-03-25 06:44:30 -05:00
|
|
|
|
2019-06-23 10:26:12 -05:00
|
|
|
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);
|
2022-07-11 06:44:55 -05:00
|
|
|
unsafe { *x = zst_val }; //~ ERROR: dereferenced after this allocation got freed
|
2019-06-23 10:26:12 -05:00
|
|
|
}
|