Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone returned byte slice in MarshalValue #1913

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package bson
import (
"bytes"
"encoding/json"
"slices"
"sync"
)

Expand Down Expand Up @@ -133,7 +134,7 @@ func MarshalValue(val interface{}) (Type, []byte, error) {
return 0, nil, err
}
typ := sw.Next(2)
return Type(typ[0]), sw.Bytes(), nil
return Type(typ[0]), slices.Clone(sw.Bytes()), nil
}

// MarshalExtJSON returns the extended JSON encoding of val.
Expand Down
58 changes: 58 additions & 0 deletions bson/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,61 @@ func TestMarshalConcurrently(t *testing.T) {
}
wg.Wait()
}

type testStruct struct {
TestData RawValue `bson:"testData"`
}

func TestSharedUseOfMarshalledBytes(t *testing.T) {
type testValue struct {
value string
}

type sharedTestCase struct {
name string
testData *testStruct
wantValue string
}

// fill the pool with some buffers
for i := 0; i < 100; i++ {
mustMarshalValue(testValue{value: fmt.Sprintf("marshalled foo bar %d", i)})
}

testCases := []sharedTestCase{
{
name: "case 1",
testData: mustMarshalValue(testValue{value: "Case 1 Value"}),
wantValue: "Case 1 Value",
},
{
name: "case 2",
testData: mustMarshalValue(testValue{value: "Case 2 Value"}),
wantValue: "Case 2 Value",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
want := mustMarshalValue(testValue{value: tc.wantValue})
if d := cmp.Diff(tc.testData, want); d != "" {
t.Errorf("diff: %s", d)
}
})
}
}

func mustMarshalValue(data any) *testStruct {
dataType, dataBytes, err := MarshalValue(data)
if err != nil {
panic(fmt.Sprintf("unable to marshal data: %s", err))
}

return &testStruct{
TestData: RawValue{
Type: dataType,
Value: dataBytes,
},
}
}
Loading