From c0ae61fa15dacfe3139c36acc21e16c4e36b668d Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 23 Oct 2023 18:34:21 +0200 Subject: [PATCH] trie: do error-checks prior to setting last/first in stacktrie --- trie/stacktrie.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/trie/stacktrie.go b/trie/stacktrie.go index fd08898b58..42bd31acc6 100644 --- a/trie/stacktrie.go +++ b/trie/stacktrie.go @@ -98,21 +98,21 @@ func (t *StackTrie) Update(key, value []byte) error { } k := keybytesToHex(key) k = k[:len(k)-1] // chop the termination flag + if bytes.Compare(t.last, k) >= 0 { + return errors.New("non-ascending key order") + } + if err := t.insert(t.root, k, value, nil); err != nil { + return err + } // track the first and last inserted entries. if t.first == nil { t.first = append([]byte{}, k...) } - if bytes.Compare(t.last, k) >= 0 { - return errors.New("non-ascending key order") - } if t.last == nil { t.last = append([]byte{}, k...) // allocate key slice } else { t.last = append(t.last[:0], k...) // reuse key slice } - if err := t.insert(t.root, k, value, nil); err != nil { - return err - } return nil }