Skip to content

Fix varint/LEB128 decoding for values > 16383#113

Merged
sevagh merged 1 commit into
sevagh:masterfrom
untitaker:varint
May 8, 2026
Merged

Fix varint/LEB128 decoding for values > 16383#113
sevagh merged 1 commit into
sevagh:masterfrom
untitaker:varint

Conversation

@untitaker

Copy link
Copy Markdown
Contributor

The shift calculation was incorrect. The old formula only worked by accident for 1-2 byte varints. For 3+ byte values, it produced garbage results. The additional tests fail without the fix.

Per the protobuf encoding spec [1] and LEB128 spec [2]:

  • Each byte contributes 7 bits of payload
  • Bytes are ordered LSB first
  • The MSB is a continuation flag

[1] https://protobuf.dev/programming-guides/encoding/
[2] https://en.wikipedia.org/wiki/LEB128

The shift calculation was incorrect. The old formula only worked by accident for 1-2 byte varints. For 3+ byte values, it produced garbage results. The additional tests fail without the fix.

Per the protobuf encoding spec [1] and LEB128 spec [2]:
- Each byte contributes 7 bits of payload
- Bytes are ordered LSB first
- The MSB is a continuation flag

[1] https://protobuf.dev/programming-guides/encoding/
[2] https://en.wikipedia.org/wiki/LEB128
@sevagh

sevagh commented May 8, 2026

Copy link
Copy Markdown
Owner

Thanks!

@sevagh sevagh merged commit 53b444a into sevagh:master May 8, 2026
@untitaker untitaker deleted the varint branch May 10, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants