From 2f372fdae551b566fa65e6400e6bb52d745f9f2a Mon Sep 17 00:00:00 2001 From: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com> Date: Wed, 30 Aug 2023 18:15:18 -0400 Subject: [PATCH] GODRIVER-2872 Duplicate slice passed to mongocrypt.newBinaryFromBytes(). (#1359) --- .../client_side_encryption_prose_test.go | 3 --- x/mongo/driver/mongocrypt/binary.go | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/mongo/integration/client_side_encryption_prose_test.go b/mongo/integration/client_side_encryption_prose_test.go index f650aa6b47..4eac5364ca 100644 --- a/mongo/integration/client_side_encryption_prose_test.go +++ b/mongo/integration/client_side_encryption_prose_test.go @@ -378,9 +378,6 @@ func TestClientSideEncryptionProse(t *testing.T) { } }) mt.Run("4. bson size limits", func(mt *mtest.T) { - // TODO(GODRIVER-2872): Fix and unskip this test case. - mt.Skip("Test fails frequently, skipping. See GODRIVER-2872") - kmsProviders := map[string]map[string]interface{}{ "local": { "key": localMasterKey, diff --git a/x/mongo/driver/mongocrypt/binary.go b/x/mongo/driver/mongocrypt/binary.go index 9e887375a9..4e4b51d74b 100644 --- a/x/mongo/driver/mongocrypt/binary.go +++ b/x/mongo/driver/mongocrypt/binary.go @@ -9,7 +9,10 @@ package mongocrypt -// #include +/* +#include +#include +*/ import "C" import ( "unsafe" @@ -17,6 +20,7 @@ import ( // binary is a wrapper type around a mongocrypt_binary_t* type binary struct { + p *C.uint8_t wrapped *C.mongocrypt_binary_t } @@ -33,11 +37,11 @@ func newBinaryFromBytes(data []byte) *binary { return newBinary() } - // We don't need C.CBytes here because data cannot go out of scope. Any mongocrypt function that takes a - // mongocrypt_binary_t will make a copy of the data so the data can be garbage collected after calling. - addr := (*C.uint8_t)(unsafe.Pointer(&data[0])) // uint8_t* - dataLen := C.uint32_t(len(data)) // uint32_t + // TODO: Consider using runtime.Pinner to replace the C.CBytes after using go1.21.0. + addr := (*C.uint8_t)(C.CBytes(data)) // uint8_t* + dataLen := C.uint32_t(len(data)) // uint32_t return &binary{ + p: addr, wrapped: C.mongocrypt_binary_new_from_data(addr, dataLen), } } @@ -52,5 +56,8 @@ func (b *binary) toBytes() []byte { // close cleans up any resources associated with the given binary instance. func (b *binary) close() { + if b.p != nil { + C.free(unsafe.Pointer(b.p)) + } C.mongocrypt_binary_destroy(b.wrapped) }