copy operands in Bytes.Add to avoid aliasing the receiver#1381
Merged
Conversation
Bytes values are immutable, but Add grew the receiver with append. When the receiver's backing array had spare capacity the second operand was written into it and the returned slice aliased the receiver, so two concatenations sharing a receiver corrupted each other. Allocate a fresh slice and copy both operands, matching list concatenation.
TristonianJones
approved these changes
Jul 23, 2026
Collaborator
|
/gcbrun |
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.
Aliased backing array in Bytes.Add
Bytes.Addgrows the receiver withappend, so when a bound bytes value's backing array has spare capacity the second operand is written into that capacity and the result aliases the receiver rather than being a fresh copy. Evaluating[b + b"\x01", b + b"\x02"]against such a value returns[b+0x02, b+0x02]because the second concatenation overwrites the first. The concatenation now allocates its own slice sized to both operands, the same immutable-view guarantee list concatenation already provides.