File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -1632,9 +1632,18 @@ class GenericReader {
1632
1632
if (RAPIDJSON_LIKELY (s.Peek () >= ' 0' && s.Peek () <= ' 9' )) {
1633
1633
exp = static_cast <int >(s.Take () - ' 0' );
1634
1634
if (expMinus) {
1635
+ // (exp + expFrac) must not underflow int => we're detecting when -exp gets
1636
+ // dangerously close to INT_MIN (a pessimistic next digit 9 would push it into
1637
+ // underflow territory):
1638
+ //
1639
+ // -(exp * 10 + 9) + expFrac >= INT_MIN
1640
+ // <=> exp <= (expFrac - INT_MIN - 9) / 10
1641
+ RAPIDJSON_ASSERT (expFrac <= 0 );
1642
+ int maxExp = (expFrac + 2147483639 ) / 10 ;
1643
+
1635
1644
while (RAPIDJSON_LIKELY (s.Peek () >= ' 0' && s.Peek () <= ' 9' )) {
1636
1645
exp = exp * 10 + static_cast <int >(s.Take () - ' 0' );
1637
- if (exp >= 214748364 ) { // Issue #313: prevent overflow exponent
1646
+ if (RAPIDJSON_UNLIKELY ( exp > maxExp)) {
1638
1647
while (RAPIDJSON_UNLIKELY (s.Peek () >= ' 0' && s.Peek () <= ' 9' )) // Consume the rest of exponent
1639
1648
s.Take ();
1640
1649
}
Original file line number Diff line number Diff line change @@ -242,6 +242,7 @@ static void TestParseDouble() {
242
242
TEST_DOUBLE (fullPrecision, " 1e-214748363" , 0.0 ); // Maximum supported negative exponent
243
243
TEST_DOUBLE (fullPrecision, " 1e-214748364" , 0.0 );
244
244
TEST_DOUBLE (fullPrecision, " 1e-21474836311" , 0.0 );
245
+ TEST_DOUBLE (fullPrecision, " 1.00000000001e-2147483638" , 0.0 );
245
246
TEST_DOUBLE (fullPrecision, " 0.017976931348623157e+310" , 1.7976931348623157e+308 ); // Max double in another form
246
247
247
248
// Since
You can’t perform that action at this time.
0 commit comments