Skip to content

Commit ae01efc

Browse files
committed
GODRIVER-725: too big integers should be parsed as doubles.
1 parent 7e0a364 commit ae01efc

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

bson/bsonrw/json_scanner.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,25 +417,23 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
417417
return nil, fmt.Errorf("invalid JSON number. Position: %d", start)
418418
case nssDone:
419419
js.pos = int(math.Max(0, float64(js.pos-1)))
420-
if t == jttDouble {
421-
v, err := strconv.ParseFloat(b.String(), 64)
422-
if err != nil {
423-
return nil, err
420+
if t != jttDouble {
421+
v, err := strconv.ParseInt(b.String(), 10, 64)
422+
if err == nil {
423+
if v < math.MinInt32 || v > math.MaxInt32 {
424+
return &jsonToken{t: jttInt64, v: v, p: start}, nil
425+
}
426+
427+
return &jsonToken{t: jttInt32, v: int32(v), p: start}, nil
424428
}
425-
426-
return &jsonToken{t: t, v: v, p: start}, nil
427429
}
428430

429-
v, err := strconv.ParseInt(b.String(), 10, 64)
431+
v, err := strconv.ParseFloat(b.String(), 64)
430432
if err != nil {
431433
return nil, err
432434
}
433435

434-
if v < math.MinInt32 || v > math.MaxInt32 {
435-
return &jsonToken{t: t, v: v, p: start}, nil
436-
}
437-
438-
return &jsonToken{t: jttInt32, v: int32(v), p: start}, nil
436+
return &jsonToken{t: jttDouble, v: v, p: start}, nil
439437
}
440438
}
441439
}

bson/bsonrw/json_scanner_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ func TestJsonScannerValidInputs(t *testing.T) {
232232
desc: "valid double: -1.2E-10", input: "-1.2E-10",
233233
tokens: []jsonToken{{t: jttDouble, v: -1.2e-10}},
234234
},
235+
{
236+
desc: "valid double: 8005332285744496613785600", input: "8005332285744496613785600",
237+
tokens: []jsonToken{{t: jttDouble, v: float64(8005332285744496613785600)}},
238+
},
235239
{
236240
desc: "valid object, only spaces",
237241
input: `{"key": "string", "key2": 2, "key3": {}, "key4": [], "key5": false }`,

0 commit comments

Comments
 (0)