Skip to content

copy operands in Bytes.Add to avoid aliasing the receiver#1381

Merged
TristonianJones merged 1 commit into
cel-expr:masterfrom
pranit0808:bytes-add-copy
Jul 23, 2026
Merged

copy operands in Bytes.Add to avoid aliasing the receiver#1381
TristonianJones merged 1 commit into
cel-expr:masterfrom
pranit0808:bytes-add-copy

Conversation

@pranit0808

Copy link
Copy Markdown
Contributor

Aliased backing array in Bytes.Add
Bytes.Add grows the receiver with append, 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.

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

Copy link
Copy Markdown
Collaborator

/gcbrun

@TristonianJones
TristonianJones merged commit 36ff97d into cel-expr:master Jul 23, 2026
8 checks passed
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