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
|
|
|
|
// (that are out-of-bounds).
|
|
|
|
let mut x_box = Box::new(1u8);
|
|
|
|
let x = (&mut *x_box as *mut u8).wrapping_offset(1);
|
2019-06-24 07:50:27 -05:00
|
|
|
// This one is just "at the edge", but still okay
|
2022-06-25 22:30:29 -05:00
|
|
|
unsafe { *(x as *mut [u8; 0]) = zst_val };
|
2019-06-23 10:26:12 -05:00
|
|
|
// One byte further is OOB.
|
|
|
|
let x = x.wrapping_offset(1);
|
2022-07-11 06:44:55 -05:00
|
|
|
unsafe { *(x as *mut [u8; 0]) = zst_val }; //~ ERROR: out-of-bounds
|
2019-06-23 10:26:12 -05:00
|
|
|
}
|