Skip to content

Commit 8d1d572

Browse files
committed
unaligned-raw-deref is always UB
1 parent 03fe377 commit 8d1d572

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This should fail even without validation or Stacked Borrows.
2+
// compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
3+
#![feature(raw_ref_macros)]
4+
use std::ptr;
5+
6+
fn main() {
7+
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
8+
let x = &x[0] as *const _ as *const u32;
9+
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
10+
// The deref is UB even if we just put the result into a raw pointer.
11+
let _x = unsafe { ptr::raw_const!(*x) }; //~ ERROR memory with alignment 2, but alignment 4 is required
12+
}

0 commit comments

Comments
 (0)