mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-04 19:12:50 +00:00
Prevent invalid values from existing in Vec::swap_remove
This commit is contained in:
parent
6162529a01
commit
0aa68a8db9
@ -1305,10 +1305,11 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
// We replace self[index] with the last element. Note that if the
|
||||
// bounds check above succeeds there must be a last element (which
|
||||
// can be self[index] itself).
|
||||
let last = ptr::read(self.as_ptr().add(len - 1));
|
||||
let hole = self.as_mut_ptr().add(index);
|
||||
let value = ptr::read(self.as_ptr().add(index));
|
||||
let base_ptr = self.as_mut_ptr();
|
||||
ptr::copy(base_ptr.add(len - 1), base_ptr.add(index), 1);
|
||||
self.set_len(len - 1);
|
||||
ptr::replace(hole, last)
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user