contain-rs / linked-hash-map

A HashMap wrapper that holds key-value pairs in insertion order
https://contain-rs.github.io/linked-hash-map/linked_hash_map/
Apache License 2.0
169 stars 60 forks source link

fix safety issue in entries() iterator #89

Closed maltek closed 6 years ago

maltek commented 7 years ago

This fixes an issue where the entries() iterator could be used to get two mutable references that alias each other, as demonstrated in this piece of code:

let mut map = LinkedHashMap::new();
map.insert(0, 0);
map.insert(1, 1);

let mut iter = map.entries();

let mut zero = iter.next().unwrap();
zero.insert(0); // moves the zero entry to the end
iter.next().unwrap(); // 1

let mut zero_alias = iter.next(); // in release mode, this silently underflows iter.remaining
assert_eq!(zero.get_mut() as *mut i32, zero_alias.unwrap().get_mut() as *mut i32);
FlashCat commented 7 years ago

Thanks for the pull request, and welcome! The contain-rs team is excited to review your changes, and you should hear from @reem (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Gankra commented 6 years ago

Thanks! (sorry, missed this)

saghm commented 6 years ago

Will a 0.5.1 be released with this fix?

Gankra commented 6 years ago

Done.