diff --git a/x/bsonx/bsoncore/bsoncore.go b/x/bsonx/bsoncore/bsoncore.go index f7997fbbb7..dde741c0a3 100644 --- a/x/bsonx/bsoncore/bsoncore.go +++ b/x/bsonx/bsoncore/bsoncore.go @@ -818,7 +818,7 @@ func readstring(src []byte) (string, []byte, bool) { if !ok { return "", src, false } - if len(src[4:]) < int(l) { + if len(src[4:]) < int(l) || l == 0 { return "", src, false } diff --git a/x/bsonx/bsoncore/value_test.go b/x/bsonx/bsoncore/value_test.go index ca0429f079..d7016b83e3 100644 --- a/x/bsonx/bsoncore/value_test.go +++ b/x/bsonx/bsoncore/value_test.go @@ -109,6 +109,11 @@ func TestValue(t *testing.T) { NewInsufficientBytesError([]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}), nil, }, + { + "StringValue/Zero Length", Value.StringValue, Value{Type: bsontype.String, Data: []byte{0x00, 0x00, 0x00, 0x00}}, + NewInsufficientBytesError([]byte{0x00, 0x00, 0x00, 0x00}, []byte{0x00, 0x00, 0x00, 0x00}), + nil, + }, { "StringValue/Success", Value.StringValue, Value{Type: bsontype.String, Data: AppendString(nil, "hello, world!")}, nil, @@ -124,6 +129,11 @@ func TestValue(t *testing.T) { nil, []interface{}{string(""), false}, }, + { + "StringValueOK/Zero Length", Value.StringValueOK, Value{Type: bsontype.String, Data: []byte{0x00, 0x00, 0x00, 0x00}}, + nil, + []interface{}{string(""), false}, + }, { "StringValueOK/Success", Value.StringValueOK, Value{Type: bsontype.String, Data: AppendString(nil, "hello, world!")}, nil,