Conversation
SPOP peer varint encoding needs up to 10 bytes for the full uint64 range (4 bits in byte 0 + 7 bits per following byte = 67 bits at 10 bytes). The 8-byte buffer used in Encode caps out at ~53 bits — any value above that makes PutUvarint return -1 and then b[:i] panics with "slice bounds out of range [:-1]". A current-day time.Now().UnixNano() trips it immediately. Unify all eight call sites in Encode to 10 bytes: numeric types (int32, uint32, int, int64, uint, uint64) and length prefixes for string and []byte. The package's own varint tests already use a 10-byte buffer, so this just makes Encode match. Regression tests from the previous commit now pass.
The previous fix bumped all eight call sites uniformly to 10 bytes, but only some need it. Right-size each case to keep allocations minimal on the hot encode path: int32, int, int64, uint, uint64 → 10 bytes (full uint64 range) uint32 → 8 bytes (max ~4.3e9 fits in 5) string / []byte length prefix → 8 bytes (len(v) never reaches 2^53) Subtle case worth noting: int32 needs 10 bytes too, but not for the obvious reason. Negative int32 values sign-extend to ~MaxUint64 when widened via uint64(v), so they hit the same overflow path as int64. Added a code comment on that line and a TestEncode_Int32_Negative regression test (round-trips int32(-1) — fails on the original 8-byte buffer with the same slice-bounds panic).
Contributor
Author
|
Hi @negasus ! Can I please get a review/approval/merge for this bug fix? Thank you! |
Contributor
Author
|
Friendly bump! |
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.
Closes #10.
typeddata.Encodeallocates an 8-byte buffer for varint encoding, but the SPOP varint format needs up to 10 bytes for the fulluint64range. When the buffer overflows,varint.PutUvarintreturns-1, the next line evaluatesb[:-1], and the worker goroutine panics withslice bounds out of range [:-1]. Easiest real-world trigger is a handler that doesSetVar(..., time.Now().UnixNano()).Less obvious: negative
int32values also panic, becauseuint64(int32(-1))sign-extends toMaxUint64and needs the full 10-byte encoding too.Fix: right-size the buffer per case rather than bumping everything uniformly, to keep allocations minimal on the hot encode path:
int32uint32int/int64/uint/uint64string/[]bytelength prefixlen(v)cannot realistically reach 2⁵³ (9 PB)