Skip to content

Commit 98345d4

Browse files
author
iwysiu
committed
GODRIVER-895 check if strLength is 0 in ReadCodeWithScope (#311)
1 parent f347d34 commit 98345d4

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

bson/bsonrw/value_reader.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ func (vr *valueReader) ReadCodeWithScope() (code string, dr DocumentReader, err
450450
if err != nil {
451451
return "", nil, err
452452
}
453+
if strLength <= 0 {
454+
return "", nil, fmt.Errorf("invalid string length: %d", strLength)
455+
}
453456
strBytes, err := vr.readBytes(strLength)
454457
if err != nil {
455458
return "", nil, err

bson/bsonrw/value_reader_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ func TestValueReader(t *testing.T) {
259259
0x0A, 0x00, // null element, empty key
260260
0x00, // document end
261261
}
262+
invalidCodeWithScope := []byte{
263+
0x7, 0x00, 0x00, 0x00, // total length
264+
0x0, 0x00, 0x00, 0x00, // string length = 0
265+
0x05, 0x00, 0x00, 0x00, 0x00, // document
266+
}
262267
testCases := []struct {
263268
name string
264269
data []byte
@@ -308,6 +313,13 @@ func TestValueReader(t *testing.T) {
308313
fmt.Errorf("length of CodeWithScope does not match lengths of components; total: %d; components: %d", 17, 19),
309314
bsontype.CodeWithScope,
310315
},
316+
{
317+
"invalid strLength",
318+
invalidCodeWithScope,
319+
0,
320+
fmt.Errorf("invalid string length: %d", 0),
321+
bsontype.CodeWithScope,
322+
},
311323
}
312324

313325
for _, tc := range testCases {

0 commit comments

Comments
 (0)