From 21968aa53b9161969a9c28e9a4deac95e0d91cc2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 10 Apr 2021 12:09:10 +0200 Subject: [PATCH] add test to detect dropped temporary --- .../dangling_pointers/stack_temporary.rs | 14 ++++++++++++++ .../storage_dead_dangling.rs | 0 2 files changed, 14 insertions(+) create mode 100644 tests/compile-fail/dangling_pointers/stack_temporary.rs rename tests/compile-fail/{ => dangling_pointers}/storage_dead_dangling.rs (100%) diff --git a/tests/compile-fail/dangling_pointers/stack_temporary.rs b/tests/compile-fail/dangling_pointers/stack_temporary.rs new file mode 100644 index 0000000000..cbd788bbf4 --- /dev/null +++ b/tests/compile-fail/dangling_pointers/stack_temporary.rs @@ -0,0 +1,14 @@ +// This should fail even without validation, but some MIR opts mask the error +// compile-flags: -Zmiri-disable-validation -Zmir-opt-level=0 + +unsafe fn make_ref<'a>(x: *mut i32) -> &'a mut i32 { + &mut *x +} + +fn main() { + unsafe { + let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"! + let val = *x; //~ ERROR dereferenced after this allocation got freed + println!("{}", val); + } +} diff --git a/tests/compile-fail/storage_dead_dangling.rs b/tests/compile-fail/dangling_pointers/storage_dead_dangling.rs similarity index 100% rename from tests/compile-fail/storage_dead_dangling.rs rename to tests/compile-fail/dangling_pointers/storage_dead_dangling.rs