triedb/pathdb: add validation for binary.Uvarint in index block operations #33140
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
This PR adds missing error checking for
binary.Uvarintcalls inhistory_index_block.goto prevent potential infinite loops and crashes when processing corrupted index block data.Problem
Missing validation in four locations can lead to serious issues:
readGreaterThan): Ignoresnreturn value when decoding restart itemreadGreaterThanloop): Non <= 0check - CRITICAL infinite loop riskreadGreaterThan): Ignoresnreturn value via binary searchscanSectionloop): Non <= 0check - CRITICAL infinite loop riskWhen
binary.Uvarintreturnsn <= 0, it indicates:n == 0: Buffer too small (incomplete varint)n < 0: Value overflow (> 64 bits)Without checks, loops can:
n == 0(pos += 0)n < 0(negative offset)Solution
Added validation following the existing pattern from lines 169-172:
For
scanSection, used silentbreakto avoid breaking the internal API.Testing
Added comprehensive tests:
TestBlockReaderCorruptedVarint: Tests all threereadGreaterThancode pathsTestBlockWriterCorruptedVarint: TestsscanSectiongraceful handlingContext
This follows recent similar fixes in the module: