diff options
| author | Ralf Jung <post@ralfj.de> | 2020-03-30 13:31:16 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2020-03-30 13:34:03 +0200 |
| commit | 5bbaac357dd85092ed0fb822947df7a4d60c1db9 (patch) | |
| tree | 12ab35b2de11f357ee0cef3711e926fd25237660 /src/liballoc/vec.rs | |
| parent | 8f479e362fbfcb31e83396ef850ab5219a32821e (diff) | |
| download | rust-5bbaac357dd85092ed0fb822947df7a4d60c1db9.tar.gz rust-5bbaac357dd85092ed0fb822947df7a4d60c1db9.zip | |
fix and test aliasing in swap_remove
Diffstat (limited to 'src/liballoc/vec.rs')
| -rw-r--r-- | src/liballoc/vec.rs | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index dc2a246f817..c600a6b649f 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -963,12 +963,13 @@ impl<T> Vec<T> { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn swap_remove(&mut self, index: usize) -> T { + assert!(index < self.len); unsafe { // We replace self[index] with the last element. Note that if the - // bounds check on hole succeeds there must be a last element (which + // bounds check above succeeds there must be a last element (which // can be self[index] itself). - let hole: *mut T = &mut self[index]; - let last = ptr::read(self.get_unchecked(self.len - 1)); + let last = ptr::read(self.as_ptr().add(self.len - 1)); + let hole: *mut T = self.as_mut_ptr().add(index); self.len -= 1; ptr::replace(hole, last) } |
