Skip to content

Commit

Permalink
Merge pull request #89 from maltek/entries-fix
Browse files Browse the repository at this point in the history
fix safety issue in entries() iterator
  • Loading branch information
Gankra authored Dec 4, 2017
2 parents 1b5ea23 + af1bc12 commit 0cdd2b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
Entries {
map: self,
head: head,
tail: self.head,
remaining: self.len(),
marker: marker::PhantomData,
}
Expand Down Expand Up @@ -844,7 +843,6 @@ pub struct IntoIter<K, V> {
pub struct Entries<'a, K: 'a, V: 'a, S: 'a = hash_map::RandomState> {
map: *mut LinkedHashMap<K, V, S>,
head: *mut Node<K, V>,
tail: *mut Node<K, V>,
remaining: usize,
marker: marker::PhantomData<(&'a K, &'a mut V, &'a S)>,
}
Expand Down Expand Up @@ -971,7 +969,7 @@ impl<'a, K, V, S: BuildHasher> Iterator for Entries<'a, K, V, S> {
type Item = OccupiedEntry<'a, K, V, S>;

fn next(&mut self) -> Option<OccupiedEntry<'a, K, V, S>> {
if self.head == self.tail {
if self.remaining == 0 {
None
} else {
self.remaining -= 1;
Expand Down
12 changes: 12 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ fn test_entries_remove() {

assert!(map.is_empty());
}
#[test]
fn entries_insert() {
let mut map = LinkedHashMap::new();
map.insert(0, 0);
map.insert(1, 1);

let mut iter = map.entries();

iter.next().unwrap().insert(0);
iter.next().unwrap(); // 1
assert!(iter.next().is_none());
}

#[test]
fn test_debug() {
Expand Down

0 comments on commit 0cdd2b5

Please sign in to comment.