Skip to content

Commit ca32743

Browse files
committed
auto merge of #10639 : jix/rust/fix_find_mut_in_trie, r=thestinger
Make TrieMap/TrieSet's find_mut check the key for external nodes. Without this find_mut sometimes returns a reference to another key when querying for a non-present key.
2 parents 09eca11 + 525878f commit ca32743

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/libstd/trie.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ fn chunk(n: uint, idx: uint) -> uint {
373373

374374
fn find_mut<'r, T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
375375
match *child {
376-
External(_, ref mut value) => Some(value),
376+
External(stored, ref mut value) if stored == key => Some(value),
377+
External(*) => None,
377378
Internal(ref mut x) => find_mut(&mut x.children[chunk(key, idx)], key, idx + 1),
378379
Nothing => None
379380
}
@@ -536,6 +537,16 @@ mod test_map {
536537
assert_eq!(m.find(&5), Some(&new));
537538
}
538539

540+
#[test]
541+
fn test_find_mut_missing() {
542+
let mut m = TrieMap::new();
543+
assert!(m.find_mut(&0).is_none());
544+
assert!(m.insert(1, 12));
545+
assert!(m.find_mut(&0).is_none());
546+
assert!(m.insert(2, 8));
547+
assert!(m.find_mut(&0).is_none());
548+
}
549+
539550
#[test]
540551
fn test_step() {
541552
let mut trie = TrieMap::new();

0 commit comments

Comments
 (0)