Skip to content
27 changes: 27 additions & 0 deletions .chloggen/pdata-copyto-deepcopy-bytes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
component: pkg/pdata

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix `pcommon.Value.CopyTo` to deep-copy `ValueTypeBytes` values instead of sharing the backing array

# One or more tracking issues or pull requests related to the change
issues: [15621]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
The bytes oneof field in CopyAnyValue was assigned directly, so the copy and source shared the same []byte.
This also affected Map.CopyTo and Slice.CopyTo, which delegate to CopyAnyValue.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
4 changes: 4 additions & 0 deletions internal/cmd/pdatagen/internal/proto/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const copyOther = `{{ if .repeated -}}
} else {
ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageName }})
}
{{ if .isBytes -}}
ov.{{ .fieldName }} = append(ov.{{ .fieldName }}[:0], t.{{ .fieldName }}...)
{{ else -}}
ov.{{ .fieldName }} = t.{{ .fieldName }}
{{ end -}}
dest.{{ .oneOfGroup }} = ov
{{ else if .nullable -}}
if src.Has{{ .fieldName }}() {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/pdatagen/internal/proto/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (pf *Field) getTemplateFields() map[string]any {
"oneOfMessageName": pf.OneOfMessageName,
"repeated": pf.Repeated,
"nullable": pf.Nullable,
"isBytes": pf.Type == TypeBytes,
"bitSize": bitSize,
"goType": pf.GoType(),
"defaultValue": pf.DefaultValue(),
Expand Down
2 changes: 1 addition & 1 deletion pdata/internal/generated_proto_anyvalue.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pdata/pcommon/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ func TestValue_CopyTo(t *testing.T) {
assert.Equal(t, internal.GenTestAnyValue(), dest.getOrig())
}

func TestValue_CopyToBytesIsDeepCopy(t *testing.T) {
src := NewValueBytes()
src.Bytes().FromRaw([]byte{1, 2, 3})

dest := NewValueEmpty()
src.CopyTo(dest)

// mutating the copy must not affect the source, since CopyTo is a deep copy.
dest.Bytes().SetAt(0, 99)
assert.Equal(t, []byte{1, 2, 3}, src.Bytes().AsRaw())
assert.Equal(t, []byte{99, 2, 3}, dest.Bytes().AsRaw())
}

func TestSliceWithNilValues(t *testing.T) {
origWithNil := []internal.AnyValue{
{},
Expand Down