Skip to content

Commit

Permalink
add test to detect dropped temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 10, 2021
1 parent aa3bc06 commit 21968aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/compile-fail/dangling_pointers/stack_temporary.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
File renamed without changes.

0 comments on commit 21968aa

Please sign in to comment.