Skip to content

Commit 9beb753

Browse files
committed
fix aliasing in drain_range
1 parent fc6664c commit 9beb753

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,15 @@ impl<A: Array> ArrayVec<A> {
600600
fn drain_range(&mut self, start: usize, end: usize) -> Drain<A>
601601
{
602602
let len = self.len();
603-
// bounds check happens here
603+
604+
// bounds check happens here (before length is changed!)
604605
let range_slice: *const _ = &self[start..end];
605606

607+
// Calling `set_len` creates a fresh and thus unique mutable references, making all
608+
// older aliases we created invalid. So we cannot call that function.
609+
self.len = Index::from(start);
610+
606611
unsafe {
607-
// set self.vec length's to start, to be safe in case Drain is leaked
608-
self.set_len(start);
609612
Drain {
610613
tail_start: end,
611614
tail_len: len - end,

0 commit comments

Comments
 (0)