fix(codec): return InvalidVarint instead of panic on excess continuation bytes#3271
Open
erenyegit wants to merge 1 commit intocommonwarexyz:mainfrom
Open
fix(codec): return InvalidVarint instead of panic on excess continuation bytes#3271erenyegit wants to merge 1 commit intocommonwarexyz:mainfrom
erenyegit wants to merge 1 commit intocommonwarexyz:mainfrom
Conversation
…ion bytes Decoder::feed could panic when fed more continuation bytes than fit in the target type (e.g. 6+ bytes for u32). Replaced unwrap() with proper Error::InvalidVarint return for malformed or adversarial input.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| } | ||
| let err = decoder.feed(0x80).unwrap_err(); | ||
| assert!(matches!(err, Error::InvalidVarint(4))); | ||
| } |
There was a problem hiding this comment.
Test doesn't exercise the new checked_sub code path
Low Severity
The test decoder_rejects_excess_continuation_bytes never exercises the new checked_sub → ok_or guard. Because feed returns an error before incrementing bits_read (line 155 is unreached on error), bits_read stays at 28 after the 5th feed. The 6th feed still computes remaining_bits = 4 via a successful checked_sub, and the error comes from the existing overflow check at lines 140–144, not the new guard. The new defensive code has no test coverage.
Additional Locations (1)
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.


Problem
varint::Decoder::feedcould panic when the decoder received more continuation bytes than fit in the target type. For example, forDecoder::<u32>(max 5 bytes), feeding a 6th byte with the continuation bit set causedmax_bits.checked_sub(self.bits_read)to returnNone, and.unwrap()panicked.This is a robustness issue: malformed or adversarial input could crash the process instead of returning an error.
Solution
Replace the
.unwrap()with.ok_or(Error::InvalidVarint(U::SIZE))?, so excess continuation bytes produce a properError::InvalidVarintinstead of a panic. The decoder already uses this error variant for other invalid varint cases.Testing
decoder_rejects_excess_continuation_bytesthat feeds 6 continuation bytes toDecoder::<u32>and asserts anInvalidVarint(4)error is returned.cargo test -p commonware-codecpasses (110 tests + doc tests).