Skip to content

Commit af1bc12

Browse files
committed
fix safety issue in entries() iterator
1 parent 1b5ea23 commit af1bc12

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
281281
Entries {
282282
map: self,
283283
head: head,
284-
tail: self.head,
285284
remaining: self.len(),
286285
marker: marker::PhantomData,
287286
}
@@ -844,7 +843,6 @@ pub struct IntoIter<K, V> {
844843
pub struct Entries<'a, K: 'a, V: 'a, S: 'a = hash_map::RandomState> {
845844
map: *mut LinkedHashMap<K, V, S>,
846845
head: *mut Node<K, V>,
847-
tail: *mut Node<K, V>,
848846
remaining: usize,
849847
marker: marker::PhantomData<(&'a K, &'a mut V, &'a S)>,
850848
}
@@ -971,7 +969,7 @@ impl<'a, K, V, S: BuildHasher> Iterator for Entries<'a, K, V, S> {
971969
type Item = OccupiedEntry<'a, K, V, S>;
972970

973971
fn next(&mut self) -> Option<OccupiedEntry<'a, K, V, S>> {
974-
if self.head == self.tail {
972+
if self.remaining == 0 {
975973
None
976974
} else {
977975
self.remaining -= 1;

tests/test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ fn test_entries_remove() {
140140

141141
assert!(map.is_empty());
142142
}
143+
#[test]
144+
fn entries_insert() {
145+
let mut map = LinkedHashMap::new();
146+
map.insert(0, 0);
147+
map.insert(1, 1);
148+
149+
let mut iter = map.entries();
150+
151+
iter.next().unwrap().insert(0);
152+
iter.next().unwrap(); // 1
153+
assert!(iter.next().is_none());
154+
}
143155

144156
#[test]
145157
fn test_debug() {

0 commit comments

Comments
 (0)