Skip to content

Commit

Permalink
clone returned byte slice in MarshalValue
Browse files Browse the repository at this point in the history
  • Loading branch information
mibo-fdc committed Dec 23, 2024
1 parent a9eddec commit 9faa056
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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,
},
}
}

0 comments on commit 9faa056

Please sign in to comment.