Skip to content

Commit 158ca2f

Browse files
committed
Fix length determination for regex
GODRIVER-893 GODRIVER-880 Change-Id: I833df440fd652ec2e079244d91edaf1b2530053d
1 parent 659b10c commit 158ca2f

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

bson/bsonrw/value_reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,12 @@ func (vr *valueReader) nextElementLength() (int32, error) {
288288
err = io.EOF
289289
break
290290
}
291-
pattern := bytes.IndexByte(vr.d[regex+1:], 0x00)
291+
pattern := bytes.IndexByte(vr.d[vr.offset+int64(regex)+1:], 0x00)
292292
if pattern < 0 {
293293
err = io.EOF
294294
break
295295
}
296-
length = int32(int64(regex) + 1 + int64(pattern) + 1 - vr.offset)
296+
length = int32(int64(regex) + 1 + int64(pattern) + 1)
297297
default:
298298
return 0, fmt.Errorf("attempted to read bytes of unknown BSON type %v", vr.stack[vr.frame].vType)
299299
}

bson/bsonrw/value_reader_test.go

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,184 +1204,185 @@ func TestValueReader(t *testing.T) {
12041204
cwsbytes := bsoncore.AppendCodeWithScope(nil, "var hellow = world;", docb)
12051205
strbytes := []byte{0x04, 0x00, 0x00, 0x00, 'f', 'o', 'o', 0x00}
12061206
testCases := []struct {
1207-
name string
1208-
t bsontype.Type
1209-
data []byte
1210-
err error
1211-
offset int64
1207+
name string
1208+
t bsontype.Type
1209+
data []byte
1210+
err error
1211+
offset int64
1212+
startingOffset int64
12121213
}{
12131214
{
12141215
"Array/invalid length",
12151216
bsontype.Array,
12161217
[]byte{0x01, 0x02, 0x03},
1217-
io.EOF, 0,
1218+
io.EOF, 0, 0,
12181219
},
12191220
{
12201221
"Array/not enough bytes",
12211222
bsontype.Array,
12221223
[]byte{0x0F, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
1223-
io.EOF, 0,
1224+
io.EOF, 0, 0,
12241225
},
12251226
{
12261227
"Array/success",
12271228
bsontype.Array,
12281229
[]byte{0x08, 0x00, 0x00, 0x00, 0x0A, '1', 0x00, 0x00},
1229-
nil, 8,
1230+
nil, 8, 0,
12301231
},
12311232
{
12321233
"EmbeddedDocument/invalid length",
12331234
bsontype.EmbeddedDocument,
12341235
[]byte{0x01, 0x02, 0x03},
1235-
io.EOF, 0,
1236+
io.EOF, 0, 0,
12361237
},
12371238
{
12381239
"EmbeddedDocument/not enough bytes",
12391240
bsontype.EmbeddedDocument,
12401241
[]byte{0x0F, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
1241-
io.EOF, 0,
1242+
io.EOF, 0, 0,
12421243
},
12431244
{
12441245
"EmbeddedDocument/success",
12451246
bsontype.EmbeddedDocument,
12461247
[]byte{0x08, 0x00, 0x00, 0x00, 0x0A, 'A', 0x00, 0x00},
1247-
nil, 8,
1248+
nil, 8, 0,
12481249
},
12491250
{
12501251
"CodeWithScope/invalid length",
12511252
bsontype.CodeWithScope,
12521253
[]byte{0x01, 0x02, 0x03},
1253-
io.EOF, 0,
1254+
io.EOF, 0, 0,
12541255
},
12551256
{
12561257
"CodeWithScope/not enough bytes",
12571258
bsontype.CodeWithScope,
12581259
[]byte{0x0F, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
1259-
io.EOF, 0,
1260+
io.EOF, 0, 0,
12601261
},
12611262
{
12621263
"CodeWithScope/success",
12631264
bsontype.CodeWithScope,
12641265
cwsbytes,
1265-
nil, 41,
1266+
nil, 41, 0,
12661267
},
12671268
{
12681269
"Binary/invalid length",
12691270
bsontype.Binary,
12701271
[]byte{0x01, 0x02, 0x03},
1271-
io.EOF, 0,
1272+
io.EOF, 0, 0,
12721273
},
12731274
{
12741275
"Binary/not enough bytes",
12751276
bsontype.Binary,
12761277
[]byte{0x0F, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
1277-
io.EOF, 0,
1278+
io.EOF, 0, 0,
12781279
},
12791280
{
12801281
"Binary/success",
12811282
bsontype.Binary,
12821283
[]byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
1283-
nil, 8,
1284+
nil, 8, 0,
12841285
},
12851286
{
12861287
"Boolean/invalid length",
12871288
bsontype.Boolean,
12881289
[]byte{},
1289-
io.EOF, 0,
1290+
io.EOF, 0, 0,
12901291
},
12911292
{
12921293
"Boolean/success",
12931294
bsontype.Boolean,
12941295
[]byte{0x01},
1295-
nil, 1,
1296+
nil, 1, 0,
12961297
},
12971298
{
12981299
"DBPointer/invalid length",
12991300
bsontype.DBPointer,
13001301
[]byte{0x01, 0x02, 0x03},
1301-
io.EOF, 0,
1302+
io.EOF, 0, 0,
13021303
},
13031304
{
13041305
"DBPointer/not enough bytes",
13051306
bsontype.DBPointer,
13061307
[]byte{0x0F, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03},
1307-
io.EOF, 0,
1308+
io.EOF, 0, 0,
13081309
},
13091310
{
13101311
"DBPointer/success",
13111312
bsontype.DBPointer,
13121313
[]byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
1313-
nil, 17,
1314+
nil, 17, 0,
13141315
},
1315-
{"DBPointer/not enough bytes", bsontype.DateTime, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0},
1316-
{"DBPointer/success", bsontype.DateTime, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8},
1317-
{"Double/not enough bytes", bsontype.Double, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0},
1318-
{"Double/success", bsontype.Double, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8},
1319-
{"Int64/not enough bytes", bsontype.Int64, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0},
1320-
{"Int64/success", bsontype.Int64, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8},
1321-
{"Timestamp/not enough bytes", bsontype.Timestamp, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0},
1322-
{"Timestamp/success", bsontype.Timestamp, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8},
1316+
{"DBPointer/not enough bytes", bsontype.DateTime, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0, 0},
1317+
{"DBPointer/success", bsontype.DateTime, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8, 0},
1318+
{"Double/not enough bytes", bsontype.Double, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0, 0},
1319+
{"Double/success", bsontype.Double, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8, 0},
1320+
{"Int64/not enough bytes", bsontype.Int64, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0, 0},
1321+
{"Int64/success", bsontype.Int64, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8, 0},
1322+
{"Timestamp/not enough bytes", bsontype.Timestamp, []byte{0x01, 0x02, 0x03, 0x04}, io.EOF, 0, 0},
1323+
{"Timestamp/success", bsontype.Timestamp, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, nil, 8, 0},
13231324
{
13241325
"Decimal128/not enough bytes",
13251326
bsontype.Decimal128,
13261327
[]byte{0x01, 0x02, 0x03, 0x04},
1327-
io.EOF, 0,
1328+
io.EOF, 0, 0,
13281329
},
13291330
{
13301331
"Decimal128/success",
13311332
bsontype.Decimal128,
13321333
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10},
1333-
nil, 16,
1334-
},
1335-
{"Int32/not enough bytes", bsontype.Int32, []byte{0x01, 0x02}, io.EOF, 0},
1336-
{"Int32/success", bsontype.Int32, []byte{0x01, 0x02, 0x03, 0x04}, nil, 4},
1337-
{"Javascript/invalid length", bsontype.JavaScript, strbytes[:2], io.EOF, 0},
1338-
{"Javascript/not enough bytes", bsontype.JavaScript, strbytes[:5], io.EOF, 0},
1339-
{"Javascript/success", bsontype.JavaScript, strbytes, nil, 8},
1340-
{"String/invalid length", bsontype.String, strbytes[:2], io.EOF, 0},
1341-
{"String/not enough bytes", bsontype.String, strbytes[:5], io.EOF, 0},
1342-
{"String/success", bsontype.String, strbytes, nil, 8},
1343-
{"Symbol/invalid length", bsontype.Symbol, strbytes[:2], io.EOF, 0},
1344-
{"Symbol/not enough bytes", bsontype.Symbol, strbytes[:5], io.EOF, 0},
1345-
{"Symbol/success", bsontype.Symbol, strbytes, nil, 8},
1346-
{"MaxKey/success", bsontype.MaxKey, []byte{}, nil, 0},
1347-
{"MinKey/success", bsontype.MinKey, []byte{}, nil, 0},
1348-
{"Null/success", bsontype.Null, []byte{}, nil, 0},
1349-
{"Undefined/success", bsontype.Undefined, []byte{}, nil, 0},
1334+
nil, 16, 0,
1335+
},
1336+
{"Int32/not enough bytes", bsontype.Int32, []byte{0x01, 0x02}, io.EOF, 0, 0},
1337+
{"Int32/success", bsontype.Int32, []byte{0x01, 0x02, 0x03, 0x04}, nil, 4, 0},
1338+
{"Javascript/invalid length", bsontype.JavaScript, strbytes[:2], io.EOF, 0, 0},
1339+
{"Javascript/not enough bytes", bsontype.JavaScript, strbytes[:5], io.EOF, 0, 0},
1340+
{"Javascript/success", bsontype.JavaScript, strbytes, nil, 8, 0},
1341+
{"String/invalid length", bsontype.String, strbytes[:2], io.EOF, 0, 0},
1342+
{"String/not enough bytes", bsontype.String, strbytes[:5], io.EOF, 0, 0},
1343+
{"String/success", bsontype.String, strbytes, nil, 8, 0},
1344+
{"Symbol/invalid length", bsontype.Symbol, strbytes[:2], io.EOF, 0, 0},
1345+
{"Symbol/not enough bytes", bsontype.Symbol, strbytes[:5], io.EOF, 0, 0},
1346+
{"Symbol/success", bsontype.Symbol, strbytes, nil, 8, 0},
1347+
{"MaxKey/success", bsontype.MaxKey, []byte{}, nil, 0, 0},
1348+
{"MinKey/success", bsontype.MinKey, []byte{}, nil, 0, 0},
1349+
{"Null/success", bsontype.Null, []byte{}, nil, 0, 0},
1350+
{"Undefined/success", bsontype.Undefined, []byte{}, nil, 0, 0},
13501351
{
13511352
"ObjectID/not enough bytes",
13521353
bsontype.ObjectID,
13531354
[]byte{0x01, 0x02, 0x03, 0x04},
1354-
io.EOF, 0,
1355+
io.EOF, 0, 0,
13551356
},
13561357
{
13571358
"ObjectID/success",
13581359
bsontype.ObjectID,
13591360
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
1360-
nil, 12,
1361+
nil, 12, 0,
13611362
},
13621363
{
13631364
"Regex/not enough bytes (first string)",
13641365
bsontype.Regex,
13651366
[]byte{'f', 'o', 'o'},
1366-
io.EOF, 0,
1367+
io.EOF, 0, 0,
13671368
},
13681369
{
13691370
"Regex/not enough bytes (second string)",
13701371
bsontype.Regex,
13711372
[]byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r'},
1372-
io.EOF, 0,
1373+
io.EOF, 0, 0,
13731374
},
13741375
{
13751376
"Regex/success",
13761377
bsontype.Regex,
1377-
[]byte{'f', 'o', 'o', 0x00, 'b', 'a', 'r', 0x00},
1378-
nil, 8,
1378+
[]byte{0x00, 0x00, 0x00, 'f', 'o', 'o', 0x00, 'i', 0x00},
1379+
nil, 9, 3,
13791380
},
13801381
{
13811382
"Unknown Type",
13821383
bsontype.Type(0),
13831384
nil,
1384-
fmt.Errorf("attempted to read bytes of unknown BSON type %v", bsontype.Type(0)), 0,
1385+
fmt.Errorf("attempted to read bytes of unknown BSON type %v", bsontype.Type(0)), 0, 0,
13851386
},
13861387
}
13871388

@@ -1394,7 +1395,8 @@ func TestValueReader(t *testing.T) {
13941395
{mode: mTopLevel},
13951396
{mode: mElement, vType: tc.t},
13961397
},
1397-
frame: 1,
1398+
frame: 1,
1399+
offset: tc.startingOffset,
13981400
}
13991401

14001402
err := vr.Skip()
@@ -1412,7 +1414,8 @@ func TestValueReader(t *testing.T) {
14121414
{mode: mTopLevel},
14131415
{mode: mElement, vType: tc.t},
14141416
},
1415-
frame: 1,
1417+
frame: 1,
1418+
offset: tc.startingOffset,
14161419
}
14171420

14181421
_, got, err := vr.ReadValueBytes(nil)
@@ -1422,8 +1425,8 @@ func TestValueReader(t *testing.T) {
14221425
if tc.err == nil && vr.offset != tc.offset {
14231426
t.Errorf("Offset not set at correct position; got %d; want %d", vr.offset, tc.offset)
14241427
}
1425-
if tc.err == nil && !bytes.Equal(got, tc.data) {
1426-
t.Errorf("Did not receive expected bytes. got %v; want %v", got, tc.data)
1428+
if tc.err == nil && !bytes.Equal(got, tc.data[tc.startingOffset:]) {
1429+
t.Errorf("Did not receive expected bytes. got %v; want %v", got, tc.data[tc.startingOffset:])
14271430
}
14281431
})
14291432
})

0 commit comments

Comments
 (0)