Skip to content

Commit f251dc4

Browse files
committed
VecDeque: fix incorrect &mut aliasing in IterMut::next/next_back
1 parent efbaa41 commit f251dc4

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

library/alloc/src/collections/vec_deque.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::cmp::{self, Ordering};
1414
use core::fmt;
1515
use core::hash::{Hash, Hasher};
1616
use core::iter::{repeat_with, FromIterator, FusedIterator};
17+
use core::marker::PhantomData;
1718
use core::mem::{self, replace, ManuallyDrop};
1819
use core::ops::{Index, IndexMut, Range, RangeBounds, Try};
1920
use core::ptr::{self, NonNull};
@@ -982,7 +983,12 @@ impl<T> VecDeque<T> {
982983
/// ```
983984
#[stable(feature = "rust1", since = "1.0.0")]
984985
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
985-
IterMut { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_mut_slice() } }
986+
IterMut {
987+
tail: self.tail,
988+
head: self.head,
989+
ring: unsafe { self.buffer_as_mut_slice() },
990+
phantom: PhantomData,
991+
}
986992
}
987993

988994
/// Returns a pair of slices which contain, in order, the contents of the
@@ -1175,6 +1181,7 @@ impl<T> VecDeque<T> {
11751181
head,
11761182
// The shared reference we have in &mut self is maintained in the '_ of IterMut.
11771183
ring: unsafe { self.buffer_as_mut_slice() },
1184+
phantom: PhantomData,
11781185
}
11791186
}
11801187

@@ -2662,15 +2669,19 @@ impl<T> FusedIterator for Iter<'_, T> {}
26622669
/// [`iter_mut`]: VecDeque::iter_mut
26632670
#[stable(feature = "rust1", since = "1.0.0")]
26642671
pub struct IterMut<'a, T: 'a> {
2665-
ring: &'a mut [T],
2672+
ring: *mut [T],
26662673
tail: usize,
26672674
head: usize,
2675+
phantom: PhantomData<&'a mut [T]>,
26682676
}
26692677

26702678
#[stable(feature = "collection_debug", since = "1.17.0")]
26712679
impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
26722680
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2673-
let (front, back) = RingSlices::ring_slices(&*self.ring, self.head, self.tail);
2681+
// FIXME: this creates a reference to the full ring, including the part
2682+
// to which we already handed out mutable references via `next()`. This
2683+
// is an aliasing violation.
2684+
let (front, back) = RingSlices::ring_slices(unsafe { &*self.ring }, self.head, self.tail);
26742685
f.debug_tuple("IterMut").field(&front).field(&back).finish()
26752686
}
26762687
}
@@ -2689,7 +2700,7 @@ impl<'a, T> Iterator for IterMut<'a, T> {
26892700

26902701
unsafe {
26912702
let elem = self.ring.get_unchecked_mut(tail);
2692-
Some(&mut *(elem as *mut _))
2703+
Some(&mut *elem)
26932704
}
26942705
}
26952706

@@ -2703,7 +2714,10 @@ impl<'a, T> Iterator for IterMut<'a, T> {
27032714
where
27042715
F: FnMut(Acc, Self::Item) -> Acc,
27052716
{
2706-
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
2717+
// FIXME: this creates a reference to the full ring, including the part
2718+
// to which we already handed out mutable references via `next()`. This
2719+
// is an aliasing violation.
2720+
let (front, back) = RingSlices::ring_slices(unsafe { &mut *self.ring }, self.head, self.tail);
27072721
accum = front.iter_mut().fold(accum, &mut f);
27082722
back.iter_mut().fold(accum, &mut f)
27092723
}
@@ -2735,15 +2749,18 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
27352749

27362750
unsafe {
27372751
let elem = self.ring.get_unchecked_mut(self.head);
2738-
Some(&mut *(elem as *mut _))
2752+
Some(&mut *elem)
27392753
}
27402754
}
27412755

27422756
fn rfold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc
27432757
where
27442758
F: FnMut(Acc, Self::Item) -> Acc,
27452759
{
2746-
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
2760+
// FIXME: this creates a reference to the full ring, including the part
2761+
// to which we already handed out mutable references via `next()`. This
2762+
// is an aliasing violation.
2763+
let (front, back) = RingSlices::ring_slices(unsafe { &mut *self.ring }, self.head, self.tail);
27472764
accum = back.iter_mut().rfold(accum, &mut f);
27482765
front.iter_mut().rfold(accum, &mut f)
27492766
}

0 commit comments

Comments
 (0)