From 7b7f690274683777e810bfb61aae2af2e36aaab8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 22 Jun 2017 17:47:09 -0700 Subject: [PATCH] Make sure that casting a ptr-integer down to u8 makes it unusable --- tests/compile-fail/ptr_int_cast.rs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/compile-fail/ptr_int_cast.rs diff --git a/tests/compile-fail/ptr_int_cast.rs b/tests/compile-fail/ptr_int_cast.rs new file mode 100644 index 000000000000..b004a18ff465 --- /dev/null +++ b/tests/compile-fail/ptr_int_cast.rs @@ -0,0 +1,7 @@ +fn main() { + let x = &1; + // Casting down to u8 and back up to a pointer loses too much precision; this must not work. + let x = x as *const i32 as u8; + let x = x as *const i32; //~ ERROR: a raw memory access tried to access part of a pointer value as raw bytes + let _ = unsafe { *x }; +}