]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/zst3.rs
Rollup merge of #102094 - GuillaumeGomez:bool-from-str-missing-docs, r=scottmcm
[rust.git] / src / tools / miri / tests / fail / zst3.rs
1 // Some optimizations remove ZST accesses, thus masking this UB.
2 //@compile-flags: -Zmir-opt-level=0
3
4 fn main() {
5     // Not using the () type here, as writes of that type do not even have MIR generated.
6     // Also not assigning directly as that's array initialization, not assignment.
7     let zst_val = [1u8; 0];
8
9     // make sure ZST accesses are checked against being "truly" dangling pointers
10     // (that are out-of-bounds).
11     let mut x_box = Box::new(1u8);
12     let x = (&mut *x_box as *mut u8).wrapping_offset(1);
13     // This one is just "at the edge", but still okay
14     unsafe { *(x as *mut [u8; 0]) = zst_val };
15     // One byte further is OOB.
16     let x = x.wrapping_offset(1);
17     unsafe { *(x as *mut [u8; 0]) = zst_val }; //~ ERROR: out-of-bounds
18 }