Skip to content

Commit

Permalink
GODRIVER-1918 Check for zero length in readstring (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis authored and Benjamin Rewis committed Mar 24, 2021
1 parent d5e11aa commit 6ea353a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/bsonx/bsoncore/bsoncore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions x/bsonx/bsoncore/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 6ea353a

Please sign in to comment.