-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
Below code when compiled with Rust 1.13 (current stable) and Rust 1.15 (current nightly) will result in segmentation faults and memory corruption. The unwrap call in the else if should consume the value and prevent further use but it does not.
fn main() {
let vec = get_vec();
if let Err(err) = vec {
println!("Got an error: {:?}", err);
}
else if vec.unwrap().len() == 0 {
println!("Vec was 0 length.");
}
else {
for i in vec.unwrap() {
println!("{}", i);
}
}
}
fn get_vec() -> Result<Vec<i32>, i32> {
Ok::<Vec<i32>, i32>(vec![1,2,3])
}
I expected: the borrow checker to mark the for loop as a use after move due to calling unwrap() after the value had been consumed by checking it in the else if statement
What happened: The borrow checker did not prevent this from compiling which resulted in segfaults and memory corruption.
Reproduction tip: You will get slightly different behavior of this code on each execution as memory errors are a bit finicky like that, if it works the first time try it a few more times, I'm sure you'll see it crash before you get to 5 attempts.
Meta
rustc --version --verbose
:
rustc 1.15.0-nightly (ac635aa 2016-11-18)
binary: rustc
commit-hash: ac635aa
commit-date: 2016-11-18
host: x86_64-pc-windows-gnu
release: 1.15.0-nightly
LLVM version: 3.9