]> git.lizzy.rs Git - rust.git/commitdiff
make sure '&raw *' on a dangling raw ptr is UB
authorRalf Jung <post@ralfj.de>
Sat, 20 Jun 2020 12:28:55 +0000 (14:28 +0200)
committerRalf Jung <post@ralfj.de>
Sat, 20 Jun 2020 12:28:55 +0000 (14:28 +0200)
tests/compile-fail/dangling_pointers/dangling_pointer_addr_of.rs [new file with mode: 0644]

diff --git a/tests/compile-fail/dangling_pointers/dangling_pointer_addr_of.rs b/tests/compile-fail/dangling_pointers/dangling_pointer_addr_of.rs
new file mode 100644 (file)
index 0000000..5df5b32
--- /dev/null
@@ -0,0 +1,13 @@
+// Make sure we find these even with many checks disabled.
+// compile-flags: -Zmiri-disable-alignment-check -Zmiri-disable-stacked-borrows -Zmiri-disable-validation
+#![feature(raw_ref_macros)]
+use std::ptr;
+
+fn main() {
+    let p = {
+        let b = Box::new(42);
+        &*b as *const i32
+    };
+    let x = unsafe { ptr::raw_const!(*p) }; //~ ERROR dereferenced after this allocation got freed
+    panic!("this should never print: {:?}", x);
+}