Skip to content

Commit 279379c

Browse files
committed
Fix potential trample in trie re-using leaf
1 parent 0955566 commit 279379c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

renderdoc/core/rdcbytetrie.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ struct rdcbytetrie
158158

159159
void SetPrefix(const Key &k)
160160
{
161+
if(IsLeaf() && k.size > GetPrefixLength())
162+
RDCERR("Expanding prefix on leaf - invalid");
163+
161164
// prefix stored immediately after this structure for both node and leaf
162165
byte *prefix = (byte *)(this + 1);
163166
// use memmove to account for prefix shrinking in place
@@ -488,11 +491,18 @@ struct rdcbytetrie
488491
promoted->SetPrefix(leaf->GetPrefix());
489492
promoted->SetValue(std::move(leaf->GetValue()));
490493

491-
// re-use the leaf later. Since we just promoted this to a node we know it will have no
492-
// children, so below we are going to hit the case of the next byte having no child and we
493-
// can put this leaf there.
494-
leaf->RemoveValue();
495-
leaf->SetPrefix(searchAfter);
494+
// re-use the leaf later if it has enough prefix space. Since we just promoted this to a
495+
// node we know it will have no children, so below we are going to hit the case of the next
496+
// byte having no child and we can put this leaf there.
497+
if(leaf->GetPrefixLength() >= searchAfter.size)
498+
{
499+
leaf->RemoveValue();
500+
leaf->SetPrefix(searchAfter);
501+
}
502+
else
503+
{
504+
leaf = MakeLeaf(searchAfter);
505+
}
496506

497507
root = promoted;
498508

renderdoc/replay/basic_types_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,8 @@ TEST_CASE("Test rdcbytetrie type", "[basictypes][rdcbytetrie]")
25932593
{{2, 1, 1}, TrieValue(5)},
25942594
//
25952595
{{2, 1, 6}, TrieValue(6)},
2596+
//
2597+
{{2, 1, 6, 9, 9}, TrieValue(7)},
25962598
};
25972599
bytebuf d = {1, 3};
25982600

0 commit comments

Comments
 (0)