Fix use-after-modify when adding new key as subset of old key

This commit is contained in:
baldurk
2025-09-03 09:39:38 +01:00
parent 283432a5dd
commit 2a87474e3c
+6 -3
View File
@@ -457,16 +457,19 @@ private:
Key prefixAfter = prefix.ExclusiveSuffixAfter(search.size);
Key prefixBefore = prefix.ExclusivePrefixBefore(search.size);
// the current node will be appended on after, so truncate its key
// the current node will be appended on as a child, it will truncate its key to prefixAfter
NodeOrLeaf *oldRoot = root;
oldRoot->SetPrefix(prefixAfter);
// create a new node with the prefix before
// this can be a small2 node as we only need one child so far
SmallNode<2> *newRoot = MakeNode<SmallNode<2>>();
newRoot->SetPrefix(prefixBefore);
root = newRoot;
// set new root prefix first as this copies into the node, both prefixBefore and prefixAfter
// reference subsets of the old prefix (which is stored in oldRoot)
newRoot->SetPrefix(prefixBefore);
oldRoot->SetPrefix(prefixAfter);
// the old root is appended on after the right child
newRoot->childBytes[0] = firstExtraKeyByte;
newRoot->children[0] = oldRoot;