trie/bintrie: add nil-resolver guards for hashed node resolution

This commit is contained in:
CPerezz 2026-04-15 22:51:53 +02:00
parent b6d385e702
commit 0f6b066156
No known key found for this signature in database
GPG key ID: 62045F34B97177DD

View file

@ -68,6 +68,9 @@ func (s *NodeStore) getSingle(ref NodeRef, stem []byte, suffix byte, resolver No
if !hasParent { if !hasParent {
return nil, errors.New("getSingle: hashed node at root") return nil, errors.New("getSingle: hashed node at root")
} }
if resolver == nil {
return nil, errors.New("getSingle: cannot resolve hashed node without resolver")
}
hn := s.getHashed(cur.Index()) hn := s.getHashed(cur.Index())
parentNode := s.getInternal(parentIdx) parentNode := s.getInternal(parentIdx)
path := makeKeyPath(int(parentNode.depth), stem) path := makeKeyPath(int(parentNode.depth), stem)
@ -138,6 +141,9 @@ func (s *NodeStore) getValuesAtStem(ref NodeRef, stem []byte, resolver NodeResol
if !hasParent { if !hasParent {
return nil, errors.New("getValuesAtStem: hashed node at root") return nil, errors.New("getValuesAtStem: hashed node at root")
} }
if resolver == nil {
return nil, errors.New("getValuesAtStem: cannot resolve hashed node without resolver")
}
hn := s.getHashed(cur.Index()) hn := s.getHashed(cur.Index())
parentNode := s.getInternal(parentIdx) parentNode := s.getInternal(parentIdx)
path := makeKeyPath(int(parentNode.depth), stem) path := makeKeyPath(int(parentNode.depth), stem)
@ -250,6 +256,9 @@ func (s *NodeStore) insertSingleInternal(stem []byte, suffix byte, value []byte,
if pathLen == 0 { if pathLen == 0 {
return errors.New("insertSingle: hashed node at root") return errors.New("insertSingle: hashed node at root")
} }
if resolver == nil {
return errors.New("insertSingleInternal: cannot resolve hashed node without resolver")
}
p := pathStack[pathLen-1] p := pathStack[pathLen-1]
parentNode := s.getInternal(p.internalIdx) parentNode := s.getInternal(p.internalIdx)
hn := s.getHashed(cur.Index()) hn := s.getHashed(cur.Index())
@ -371,6 +380,9 @@ func (s *NodeStore) insertValuesAtStem(ref NodeRef, stem []byte, values [][]byte
bit := stem[node.depth/8] >> (7 - (node.depth % 8)) & 1 bit := stem[node.depth/8] >> (7 - (node.depth % 8)) & 1
if bit == 0 { if bit == 0 {
if node.left.Kind() == KindHashed { if node.left.Kind() == KindHashed {
if resolver == nil {
return ref, errors.New("insertValuesAtStem: cannot resolve hashed node without resolver")
}
hn := s.getHashed(node.left.Index()) hn := s.getHashed(node.left.Index())
path := makeKeyPath(int(node.depth), stem) path := makeKeyPath(int(node.depth), stem)
data, err := resolver(path, hn.hash) data, err := resolver(path, hn.hash)
@ -391,6 +403,9 @@ func (s *NodeStore) insertValuesAtStem(ref NodeRef, stem []byte, values [][]byte
node.left = newChild node.left = newChild
} else { } else {
if node.right.Kind() == KindHashed { if node.right.Kind() == KindHashed {
if resolver == nil {
return ref, errors.New("insertValuesAtStem: cannot resolve hashed node without resolver")
}
hn := s.getHashed(node.right.Index()) hn := s.getHashed(node.right.Index())
path := makeKeyPath(int(node.depth), stem) path := makeKeyPath(int(node.depth), stem)
data, err := resolver(path, hn.hash) data, err := resolver(path, hn.hash)