Skip to content

Commit

Permalink
Rollup merge of rust-lang#61082 - RalfJung:vec, r=alexcrichton
Browse files Browse the repository at this point in the history
fix dangling reference in Vec::append

Turns out I forgot to enable Miri again for the Vec tests. And there was a dangling reference hiding in there!  `get_unchecked_mut` is UB to call on an empty vector (there is no memory to get a reference to), and yet this code did it.
  • Loading branch information
Centril committed May 23, 2019
2 parents 26f3528 + f44b264 commit ee97210
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/liballoc/tests/vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(not(miri))]

use std::borrow::Cow;
use std::mem::size_of;
use std::{usize, isize};
Expand Down Expand Up @@ -763,6 +761,7 @@ fn from_into_inner() {
it.next().unwrap();
let vec = it.collect::<Vec<_>>();
assert_eq!(vec, [2, 3]);
#[cfg(not(miri))] // Miri does not support comparing dangling pointers
assert!(ptr != vec.as_ptr());
}

Expand Down Expand Up @@ -971,6 +970,7 @@ fn test_reserve_exact() {
}

#[test]
#[cfg(not(miri))] // Miri does not support signalling OOM
fn test_try_reserve() {

// These are the interesting cases:
Expand Down Expand Up @@ -1073,6 +1073,7 @@ fn test_try_reserve() {
}

#[test]
#[cfg(not(miri))] // Miri does not support signalling OOM
fn test_try_reserve_exact() {

// This is exactly the same as test_try_reserve with the method changed.
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ impl<T> Vec<T> {
let count = (*other).len();
self.reserve(count);
let len = self.len();
ptr::copy_nonoverlapping(other as *const T, self.get_unchecked_mut(len), count);
ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count);
self.len += count;
}

Expand Down

0 comments on commit ee97210

Please sign in to comment.