about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorJannis Harder <jix@jixco.de>2013-11-24 23:41:33 +0100
committerJannis Harder <jix@jixco.de>2013-11-25 00:09:40 +0100
commit525878fc96deb41bdc8f68daf5ba686688889d79 (patch)
tree25929fb53ef25b4e61316ba731da40a5b24d5ae2 /src/libstd
parentb3ff24adaa8c7f9c48c525f284526c23ffd33fcb (diff)
downloadrust-525878fc96deb41bdc8f68daf5ba686688889d79.tar.gz
rust-525878fc96deb41bdc8f68daf5ba686688889d79.zip
std::trie: Fix find_mut for non-present keys
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/trie.rs13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/libstd/trie.rs b/src/libstd/trie.rs
index f60d8641aca..98f5baf1e2b 100644
--- a/src/libstd/trie.rs
+++ b/src/libstd/trie.rs
@@ -373,7 +373,8 @@ fn chunk(n: uint, idx: uint) -> uint {
 
 fn find_mut<'r, T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
     match *child {
-        External(_, ref mut value) => Some(value),
+        External(stored, ref mut value) if stored == key => Some(value),
+        External(*) => None,
         Internal(ref mut x) => find_mut(&mut x.children[chunk(key, idx)], key, idx + 1),
         Nothing => None
     }
@@ -537,6 +538,16 @@ mod test_map {
     }
 
     #[test]
+    fn test_find_mut_missing() {
+        let mut m = TrieMap::new();
+        assert!(m.find_mut(&0).is_none());
+        assert!(m.insert(1, 12));
+        assert!(m.find_mut(&0).is_none());
+        assert!(m.insert(2, 8));
+        assert!(m.find_mut(&0).is_none());
+    }
+
+    #[test]
     fn test_step() {
         let mut trie = TrieMap::new();
         let n = 300u;