diff options
author | obscuren <geffobscura@gmail.com> | 2014-07-15 21:29:54 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-07-15 21:29:54 +0800 |
commit | 09bade64666f82a2580e7d24a8bc7655e2113287 (patch) | |
tree | f0014dd11139789289bb68ebee8d2da52078e12b /ethtrie/trie.go | |
parent | 5ec62a51537dec6cd299dfc0ff1fbd0847338ff8 (diff) | |
download | dexon-09bade64666f82a2580e7d24a8bc7655e2113287.tar dexon-09bade64666f82a2580e7d24a8bc7655e2113287.tar.gz dexon-09bade64666f82a2580e7d24a8bc7655e2113287.tar.bz2 dexon-09bade64666f82a2580e7d24a8bc7655e2113287.tar.lz dexon-09bade64666f82a2580e7d24a8bc7655e2113287.tar.xz dexon-09bade64666f82a2580e7d24a8bc7655e2113287.tar.zst dexon-09bade64666f82a2580e7d24a8bc7655e2113287.zip |
Fixed an issue where the trie might crash on missmatching lengths
Diffstat (limited to 'ethtrie/trie.go')
-rw-r--r-- | ethtrie/trie.go | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/ethtrie/trie.go b/ethtrie/trie.go index 38c78e7f4..07720be54 100644 --- a/ethtrie/trie.go +++ b/ethtrie/trie.go @@ -9,6 +9,8 @@ import ( "sync" ) +func __ignore() { fmt.Println("") } + func ParanoiaCheck(t1 *Trie) (bool, *Trie) { t2 := NewTrie(ethutil.Config.Db, "") @@ -269,8 +271,7 @@ func (t *Trie) getState(node interface{}, key []int) interface{} { } // It shouldn't come this far - fmt.Println("getState unexpected return") - return "" + panic("unexpected return") } func (t *Trie) getNode(node interface{}) *ethutil.Value { @@ -287,7 +288,9 @@ func (t *Trie) getNode(node interface{}) *ethutil.Value { return ethutil.NewValueFromBytes([]byte(str)) } - return t.cache.Get(n.Bytes()) + data := t.cache.Get(n.Bytes()) + + return data } func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{} { @@ -385,7 +388,7 @@ func (t *Trie) InsertState(node interface{}, key []int, value interface{}) inter return t.Put(newNode) } - return "" + panic("unexpected end") } func (t *Trie) deleteState(node interface{}, key []int) interface{} { @@ -396,6 +399,7 @@ func (t *Trie) deleteState(node interface{}, key []int) interface{} { // New node n := ethutil.NewValue(node) if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 { + //return nil return "" } @@ -406,12 +410,17 @@ func (t *Trie) deleteState(node interface{}, key []int) interface{} { k := CompactDecode(currentNode.Get(0).Str()) v := currentNode.Get(1).Raw() + matchingLength := MatchingNibbleLength(key, k) + // Matching key pair (ie. there's already an object with this key) if CompareIntSlice(k, key) { return "" - } else if CompareIntSlice(key[:len(k)], k) { + } else if CompareIntSlice(key[:matchingLength], k) { hash := t.deleteState(v, key[len(k):]) child := t.getNode(hash) + if child.IsNil() { + return node + } var newNode []interface{} if child.Len() == 2 { |