Skip to content

Commit 801470d

Browse files
authored
GODRIVER-1918 Check for zero length in readstring (#613)
1 parent caeb478 commit 801470d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

x/bsonx/bsoncore/bsoncore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ func readstring(src []byte) (string, []byte, bool) {
818818
if !ok {
819819
return "", src, false
820820
}
821-
if len(src[4:]) < int(l) {
821+
if len(src[4:]) < int(l) || l == 0 {
822822
return "", src, false
823823
}
824824

x/bsonx/bsoncore/value_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func TestValue(t *testing.T) {
109109
NewInsufficientBytesError([]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}),
110110
nil,
111111
},
112+
{
113+
"StringValue/Zero Length", Value.StringValue, Value{Type: bsontype.String, Data: []byte{0x00, 0x00, 0x00, 0x00}},
114+
NewInsufficientBytesError([]byte{0x00, 0x00, 0x00, 0x00}, []byte{0x00, 0x00, 0x00, 0x00}),
115+
nil,
116+
},
112117
{
113118
"StringValue/Success", Value.StringValue, Value{Type: bsontype.String, Data: AppendString(nil, "hello, world!")},
114119
nil,
@@ -124,6 +129,11 @@ func TestValue(t *testing.T) {
124129
nil,
125130
[]interface{}{string(""), false},
126131
},
132+
{
133+
"StringValueOK/Zero Length", Value.StringValueOK, Value{Type: bsontype.String, Data: []byte{0x00, 0x00, 0x00, 0x00}},
134+
nil,
135+
[]interface{}{string(""), false},
136+
},
127137
{
128138
"StringValueOK/Success", Value.StringValueOK, Value{Type: bsontype.String, Data: AppendString(nil, "hello, world!")},
129139
nil,

0 commit comments

Comments
 (0)