rust/tests/fail/validity/transmute_through_ptr.rs

22 lines
577 B
Rust
Raw Normal View History

#[repr(u32)]
#[derive(Debug)]
enum Bool {
True,
}
fn evil(x: &mut Bool) {
let x = x as *mut _ as *mut u32;
unsafe {
2022-06-21 13:40:02 -05:00
*x = 44; // out-of-bounds enum tag
}
}
#[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/5391
fn main() {
let mut x = Bool::True;
evil(&mut x);
let y = x; // reading this ought to be enough to trigger validation
2021-06-15 03:11:49 -05:00
//~^ ERROR type validation failed at .<enum-tag>: encountered 0x0000002c, but expected a valid enum tag
println!("{:?}", y); // make sure it is used (and not optimized away)
}