fix(encoding): throw on buffer overflow instead of silent truncation in encodeVarint (#7147)#7159
Open
LeSingh1 wants to merge 1 commit into
Open
fix(encoding): throw on buffer overflow instead of silent truncation in encodeVarint (#7147)#7159LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
…enoland#7147) `encodeVarint` walked the loop with `i <= Math.min(buf.length, MaxVarintLen64)`. For a value that needed all 10 uint64 varint bytes plus a continuation (e.g. `0x1234567891234567891n`), the 11th iteration wrote to `buf[10]` on the default 10-byte buffer — silently dropped by `Uint8Array` — and the function returned `[Uint8Array(10), 11]`, a slice shorter than the reported byte count. Callers then handed that truncated buffer to `decodeVarint` and got a wrong number back. Cap the loop at `min(buf.length - offset, MaxVarintLen64)`, write through `offset + i` so the offset path can't drift either, and split the post-loop throw into two messages: "buffer holds at most N byte(s) after offset" when the user supplied (or defaulted to) a short buffer, and the existing "overflows uint64" when the value genuinely exceeds the protocol limit. The success path returns identical results to before. Three new tests in `encoding/varint_test.ts`: the issue's exact 76-bit value (must throw "overflows uint64"), a 3-byte value into a 2-byte buffer (must throw "buffer holds at most 2 bytes"), and offset == buf length (must throw "buffer holds at most 0 bytes"). Full suite stays green: 18 passed.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7159 +/- ##
=======================================
Coverage 94.57% 94.57%
=======================================
Files 636 636
Lines 52138 52141 +3
Branches 9399 9400 +1
=======================================
+ Hits 49311 49314 +3
Misses 2249 2249
Partials 578 578 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Fixes #7147.
encodeVarintwalked the loop withi <= Math.min(buf.length, MaxVarintLen64). For a value that needed all 10 uint64 varint bytes plus a continuation, the 11th iteration wrote tobuf[10]on the default 10-byte buffer — silently dropped byUint8Array— and the function returned[Uint8Array(10), 11], a slice shorter than the byte count it reported. Callers then fed that truncated buffer todecodeVarintand got a wrong number back. Repro from the issue:Fix
min(buf.length - offset, MaxVarintLen64), write throughoffset + iso the offset path can't drift either."buffer holds at most N byte(s) after offset"when the caller (or default) supplied a short buffer, and the existing"overflows uint64"when the value genuinely exceeds the protocol limit. Both areRangeErrorso existingassertThrows(_, RangeError)callsites still pass.Tests
Three cases added to
encoding/varint_test.ts:throws when the default buffer is too small (#7147)— exact repro from issue, assertsRangeErrorwith"overflows uint64"throws when a caller-provided buffer is too small— 200000 (3 varint bytes) into a 2-byte bufferthrows when offset leaves no room in the buffer— 10-byte buffer withoffset=10