Skip to content

Commit f7134aa

Browse files
committed
GODRIVER-2872 Duplicate slice passed to mongocrypt.newBinaryFromBytes(). (#1359)
1 parent d2f8316 commit f7134aa

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

mongo/integration/client_side_encryption_prose_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,6 @@ func TestClientSideEncryptionProse(t *testing.T) {
379379
}
380380
})
381381
mt.Run("4. bson size limits", func(mt *mtest.T) {
382-
// TODO(GODRIVER-2872): Fix and unskip this test case.
383-
mt.Skip("Test fails frequently, skipping. See GODRIVER-2872")
384-
385382
kmsProviders := map[string]map[string]interface{}{
386383
"local": {
387384
"key": localMasterKey,

x/mongo/driver/mongocrypt/binary.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99

1010
package mongocrypt
1111

12-
// #include <mongocrypt.h>
12+
/*
13+
#include <stdlib.h>
14+
#include <mongocrypt.h>
15+
*/
1316
import "C"
1417
import (
1518
"unsafe"
1619
)
1720

1821
// binary is a wrapper type around a mongocrypt_binary_t*
1922
type binary struct {
23+
p *C.uint8_t
2024
wrapped *C.mongocrypt_binary_t
2125
}
2226

@@ -33,11 +37,11 @@ func newBinaryFromBytes(data []byte) *binary {
3337
return newBinary()
3438
}
3539

36-
// We don't need C.CBytes here because data cannot go out of scope. Any mongocrypt function that takes a
37-
// mongocrypt_binary_t will make a copy of the data so the data can be garbage collected after calling.
38-
addr := (*C.uint8_t)(unsafe.Pointer(&data[0])) // uint8_t*
39-
dataLen := C.uint32_t(len(data)) // uint32_t
40+
// TODO: Consider using runtime.Pinner to replace the C.CBytes after using go1.21.0.
41+
addr := (*C.uint8_t)(C.CBytes(data)) // uint8_t*
42+
dataLen := C.uint32_t(len(data)) // uint32_t
4043
return &binary{
44+
p: addr,
4145
wrapped: C.mongocrypt_binary_new_from_data(addr, dataLen),
4246
}
4347
}
@@ -52,5 +56,8 @@ func (b *binary) toBytes() []byte {
5256

5357
// close cleans up any resources associated with the given binary instance.
5458
func (b *binary) close() {
59+
if b.p != nil {
60+
C.free(unsafe.Pointer(b.p))
61+
}
5562
C.mongocrypt_binary_destroy(b.wrapped)
5663
}

0 commit comments

Comments
 (0)