fix(go): ensure physical buffer space for unsafe varint fast-paths#3613
Merged
chaokunyang merged 2 commits intoapache:mainfrom Apr 24, 2026
Merged
fix(go): ensure physical buffer space for unsafe varint fast-paths#3613chaokunyang merged 2 commits intoapache:mainfrom
chaokunyang merged 2 commits intoapache:mainfrom
Conversation
Contributor
Author
|
@chaokunyang have a look. |
chaokunyang
reviewed
Apr 24, 2026
| // TestUnsafePutVarUint32PhysicalWriteWidth verifies that UnsafePutVarUint32 performs | ||
| // an 8-byte physical write for 5-byte varints and that Reserve(8) (as required by | ||
| // buffer.go:661) keeps those 8 bytes within the backing array. | ||
| func TestUnsafePutVarUint32PhysicalWriteWidth(t *testing.T) { |
Collaborator
There was a problem hiding this comment.
Please add another test to cover struct.go change
Contributor
Author
|
@chaokunyang please have a look. |
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.
Why?
The Go runtime's struct serialization fast-path in
struct.goviolates the documented contract ofUnsafePutVarUint32andUnsafeReadVarUint32inbuffer.go.UnsafePutVarUint32documents that the caller must callReserve(8)(because it performs an 8-byte bulk write for 5-byte varints), butstruct.goonly callsReserve(MaxVarintSize), which is 5 for uint32/int32 varint fields.UnsafeReadVarUint32physically reads 8 bytes, but the fast-path guard instruct.goonly checksremaining() >= MaxVarintSize(which can be 5).What does this PR do?
Add +8 byte padding to struct varint reservation and remaining-check guardrails in struct.go. This ensures that the underlying unsafe bulk memory operations (8-byte loads/stores) always stay within the allocated backing array, even when the logical varint size is smaller (e.g., 5 bytes).
Includes a regression test in buffer_test.go verifying the physical write width of
UnsafePutVarUint32.Related issues
Closes #3612
AI Contribution Checklist
yes/noyes, I included a completed AI Contribution Checklist in this PR description and the requiredAI Usage Disclosure.yes, my PR description includes the requiredai_reviewsummary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes.Does this PR introduce any user-facing change?
Benchmark