From 99520432ecb6f9274ca632eb4d3876b23eb37ae3 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Sun, 19 Apr 2026 22:14:31 +0200 Subject: [PATCH] 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. --- trie/bintrie/store_ops.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/trie/bintrie/store_ops.go b/trie/bintrie/store_ops.go index ad47347986..23e3e4520e 100644 --- a/trie/bintrie/store_ops.go +++ b/trie/bintrie/store_ops.go @@ -278,6 +278,11 @@ func (s *NodeStore) splitStemValuesInsert(existingRef nodeRef, newStem []byte, v } newChild, err := s.insertValuesAtStem(child, newStem, values, resolver, depth+1) 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 } if bitStem == 0 {