Fix TrieDBMut panic when removing a strict-prefix key#232
Open
dimartiro wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Removing a key that is only a strict prefix of existing keys panics in debug builds and silently corrupts the trie in release builds, under any no-extension layout (including the substrate-like hashed-value codec used in production).
Closes #217.
Reproduction
Root cause
ffis not present but is a strict prefix offfff/ffffff. While descending, the rootNibbledBranchconsumes the whole search key viakey.advance(common + 1), so the recursion reaches the innerNibbledBranch(whose own partial the key never traversed) with an empty remaining key.The
(Node::NibbledBranch(n, children, val), true)arm inremove_inspectortreated "empty remaining key" as "the value lives here". It removed a different (longer) key's value and calledfix, whosefix_inneradvances the nibble offset by the branch's own partial length on an already-exhausted key — trippingdebug_assert!(self.len() >= i)inNibbleSlice::advance(panic in debug; in release the offset overflows and a valid value is dropped).Fix
Guard the value removal on the branch's own partial being empty. A
NibbledBranch's value is reachable only by a key that still contains the whole of its partial, so an empty remaining key with a non-empty partial means the key terminated early and is absent — removal must be a no-op (Action::Restore).Testing
Two regression tests, run across all reference layouts via
test_layouts!(includingHashedValueNoExtThreshold, the substrate-like hashed-value codec):remove_prefix_key_is_noop_issue_217— removing the absent prefix key leaves the trie root byte-identical and all existing values intact.remove_exact_branch_value_issue_217— a value genuinely sitting on a partial-bearing branch is still removable (the guard must not over-skip legitimate removals).Full
trie-db-testsuite passes (126/126).