trie/bintrie: add depth bound to splitStemInsert

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

View file

@ -302,14 +302,15 @@ func (s *NodeStore) splitStemInsert(existingRef NodeRef, newStem []byte, suffix
first := true
for {
if existingDepth >= StemSize*8 {
panic("splitStemInsert: identical stems")
}
bitExisting := existing.Stem[existingDepth/8] >> (7 - (existingDepth % 8)) & 1
bitNew := newStem[existingDepth/8] >> (7 - (existingDepth % 8)) & 1
newInternalIdx := s.allocInternal()
newInternal := s.getInternal(newInternalIdx)
newInternal.depth = uint8(existingDepth)
newInternal.mustRecompute = true
newRef := MakeRef(KindInternal, newInternalIdx)
newRef := s.newInternalRef(existingDepth)
newInternal := s.getInternal(newRef.Index())
if first {
firstRef = newRef
@ -346,7 +347,7 @@ func (s *NodeStore) splitStemInsert(existingRef NodeRef, newStem []byte, suffix
}
// Same bit — continue splitting
lastInternalIdx = newInternalIdx
lastInternalIdx = newRef.Index()
lastIsLeft = (bitExisting == 0)
existingDepth++
}
@ -473,6 +474,10 @@ func (s *NodeStore) insertValuesAtStem(ref NodeRef, stem []byte, values [][]byte
func (s *NodeStore) splitStemValuesInsert(existingRef NodeRef, newStem []byte, values [][]byte, resolver NodeResolverFn, depth int) (NodeRef, error) {
existing := s.getStem(existingRef.Index())
if int(existing.depth) >= StemSize*8 {
panic("splitStemValuesInsert: identical stems")
}
bitStem := existing.Stem[existing.depth/8] >> (7 - (existing.depth % 8)) & 1
nRef := s.newInternalRef(int(existing.depth))
nNode := s.getInternal(nRef.Index())