Skip to content

Commit 6a3dce9

Browse files
committed
Auto merge of #85814 - steffahn:fix_linked_list_itermut_debug, r=m-ou-se
Fix unsoundness of Debug implementation for linked_list::IterMut Fix #85813, new `marker` field follows the example of `linked_list::Iter`.
2 parents d9feaaa + b4dcdb4 commit 6a3dce9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

library/alloc/src/collections/linked_list.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ pub struct Iter<'a, T: 'a> {
6464
#[stable(feature = "collection_debug", since = "1.17.0")]
6565
impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
6666
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
f.debug_tuple("Iter").field(&self.len).finish()
67+
f.debug_tuple("Iter")
68+
.field(&*mem::ManuallyDrop::new(LinkedList {
69+
head: self.head,
70+
tail: self.tail,
71+
len: self.len,
72+
marker: PhantomData,
73+
}))
74+
.field(&self.len)
75+
.finish()
6876
}
6977
}
7078

@@ -82,19 +90,24 @@ impl<T> Clone for Iter<'_, T> {
8290
/// documentation for more.
8391
#[stable(feature = "rust1", since = "1.0.0")]
8492
pub struct IterMut<'a, T: 'a> {
85-
// We do *not* exclusively own the entire list here, references to node's `element`
86-
// have been handed out by the iterator! So be careful when using this; the methods
87-
// called must be aware that there can be aliasing pointers to `element`.
88-
list: &'a mut LinkedList<T>,
8993
head: Option<NonNull<Node<T>>>,
9094
tail: Option<NonNull<Node<T>>>,
9195
len: usize,
96+
marker: PhantomData<&'a mut Node<T>>,
9297
}
9398

9499
#[stable(feature = "collection_debug", since = "1.17.0")]
95100
impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
96101
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97-
f.debug_tuple("IterMut").field(&self.list).field(&self.len).finish()
102+
f.debug_tuple("IterMut")
103+
.field(&*mem::ManuallyDrop::new(LinkedList {
104+
head: self.head,
105+
tail: self.tail,
106+
len: self.len,
107+
marker: PhantomData,
108+
}))
109+
.field(&self.len)
110+
.finish()
98111
}
99112
}
100113

@@ -493,7 +506,7 @@ impl<T> LinkedList<T> {
493506
#[inline]
494507
#[stable(feature = "rust1", since = "1.0.0")]
495508
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
496-
IterMut { head: self.head, tail: self.tail, len: self.len, list: self }
509+
IterMut { head: self.head, tail: self.tail, len: self.len, marker: PhantomData }
497510
}
498511

499512
/// Provides a cursor at the front element.

0 commit comments

Comments
 (0)