Skip to content

Commit 0aa68a8

Browse files
committed
Prevent invalid values from existing in Vec::swap_remove
1 parent 6162529 commit 0aa68a8

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

library/alloc/src/vec/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1305,10 +1305,11 @@ impl<T, A: Allocator> Vec<T, A> {
13051305
// We replace self[index] with the last element. Note that if the
13061306
// bounds check above succeeds there must be a last element (which
13071307
// can be self[index] itself).
1308-
let last = ptr::read(self.as_ptr().add(len - 1));
1309-
let hole = self.as_mut_ptr().add(index);
1308+
let value = ptr::read(self.as_ptr().add(index));
1309+
let base_ptr = self.as_mut_ptr();
1310+
ptr::copy(base_ptr.add(len - 1), base_ptr.add(index), 1);
13101311
self.set_len(len - 1);
1311-
ptr::replace(hole, last)
1312+
value
13121313
}
13131314
}
13141315

0 commit comments

Comments
 (0)