trie/bintrie: roll back split-stem depth on error

splitStemValuesInsert increments existing.depth before recursing into
insertValuesAtStem. If the recursion fails, the depth stays incremented
but the tree is not re-rooted through the new internal, so a retry
reads bitStem at the wrong offset and can place the stem on the wrong
side of a fresh split. Roll back existing.depth on error to keep the
stem consistent across retries.
This commit is contained in:
CPerezz 2026-04-19 22:14:31 +02:00
parent 7f20e908e2
commit 99520432ec
No known key found for this signature in database
GPG key ID: 62045F34B97177DD

View file

@ -278,6 +278,11 @@ func (s *NodeStore) splitStemValuesInsert(existingRef nodeRef, newStem []byte, v
} }
newChild, err := s.insertValuesAtStem(child, newStem, values, resolver, depth+1) newChild, err := s.insertValuesAtStem(child, newStem, values, resolver, depth+1)
if err != nil { if err != nil {
// Roll back the depth increment so a retry sees the same
// existing state and extracts bitStem at the correct offset.
// nRef itself leaks (no internal free-list), but the slot is
// unreachable from the tree and harmless.
existing.depth--
return nRef, err return nRef, err
} }
if bitStem == 0 { if bitStem == 0 {