Repository navigation
fix(wire): reject over-long and over-large varints in ConsumeVarint - #31
Open
linhongyu510 wants to merge 1 commit into
Open
linhongyu510 wants to merge 1 commit into
linhongyu510 wants to merge 1 commit into
Conversation
|
|
linhongyu510
force-pushed
the
fix/varint-overflow-check
branch
from
October 8, 2026 09:25
17e129b to
47e6ca2
Compare
ConsumeVarint previously had no length cap and no 10th-byte validation, diverging from the reference google.golang.org/protobuf/encoding/protowire decoder: - A 10-byte varint whose 10th byte had value >= 2 was silently accepted with corrupted bits (shift-by >= 64 wraps modulo 64, ORing stray bits). - An 11+ byte varint was silently parsed, with later bytes ORing into already-used bit positions. The reference decoder returns errCodeOverflow (-3) in both cases. Fix: limit the scan loop to the first 9 bytes (the hot path for all field tags and most values), then check the 10th byte separately. The 10th byte can only contribute bit 63 (value 0 or 1); any larger value or a 10th continuation byte returns -3. Performance: benchstat (3 runs each, arm64) shows no regression on 1-5 byte varints (within noise or slightly faster); the loop-bound hint helps the compiler optimize the short path.
linhongyu510
force-pushed
the
fix/varint-overflow-check
branch
from
October 8, 2026 09:39
47e6ca2 to
04a6bd0
Compare
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
ConsumeVarintdiverged from the referencegoogle.golang.org/protobuf/encoding/protowiredecoder:No 10-byte cap. The loop reads continuation bytes indefinitely. At i=10+,
i*7 >= 70\], and Go's unsigned shift wraps modulo 64, so later bytes OR stray bits into already-populated positions ofv`.No 10th-byte value check. The reference requires the 10th byte's lower 7 bits to be 0 or 1 (only bit 63 can be set in a 64-bit varint). FastPB accepted any value.
Reproduction (before fix)
[0x80×9, 0x02](10B, b[9]=2)(v=0, n=10)accepted(v=0, n=-3)overflow[0x80×9, 0x7f](10B, b[9]=127)(v=2^63, n=10)wrong value(v=0, n=-3)overflow[0x80×10, 0x01](11B)(v=0, n=11)accepted(v=0, n=-3)overflow[0x80×9, 0x01](max uint64)(v=2^63, n=10)(v=2^63, n=10)✓ matchFix
Limit the scan loop to the first 9 bytes (the hot path for all field tags and most values), then check the 10th byte separately. The 10th byte can only contribute bit 63 (value 0 or 1); any larger value or a 10th continuation byte returns
-3(errCodeOverflow).This matches the reference protowire structure: the short-path loop body is unchanged from the original (no extra per-iteration comparison).
Performance
Benchstat (3 runs each, Apple M5 Pro / arm64, 2s benchtime):
No hot-path regression. The
i < 9loop bound gives the compiler a known small trip count, which improves codegen.Verification
go build ./...clean (amd64, arm64, 386)go vetcleango test .all passgo test -race .clean