Commit 735354ef authored by Milo Yip's avatar Milo Yip

Separate handling for pos/neg exp and improve pos exp overflow

parent 93d13ad2
...@@ -925,14 +925,21 @@ private: ...@@ -925,14 +925,21 @@ private:
if (s.Peek() >= '0' && s.Peek() <= '9') { if (s.Peek() >= '0' && s.Peek() <= '9') {
exp = s.Take() - '0'; exp = s.Take() - '0';
while (s.Peek() >= '0' && s.Peek() <= '9') { if (expMinus) {
exp = exp * 10 + (s.Take() - '0'); while (s.Peek() >= '0' && s.Peek() <= '9') {
if (exp > 308 && !expMinus) // exp > 308 should be rare, so it should be checked first. exp = exp * 10 + (s.Take() - '0');
RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, s.Tell()); if (exp >= 429496729) { // Issue #313: prevent overflow exponent
else if (exp >= 429496729 && expMinus) { // Issue #313: prevent overflow exponent while (s.Peek() >= '0' && s.Peek() <= '9') // Consume the rest of exponent
while (s.Peek() >= '0' && s.Peek() <= '9') // Consume the rest of exponent s.Take();
s.Take(); }
break; }
}
else { // positive exp
int maxExp = 308 - expFrac;
while (s.Peek() >= '0' && s.Peek() <= '9') {
exp = exp * 10 + (s.Take() - '0');
if (exp > maxExp)
RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, s.Tell());
} }
} }
} }
......
...@@ -229,6 +229,7 @@ static void TestParseDouble() { ...@@ -229,6 +229,7 @@ static void TestParseDouble() {
TEST_DOUBLE(fullPrecision, "1e-00011111111111", 0.0); // Issue #313 TEST_DOUBLE(fullPrecision, "1e-00011111111111", 0.0); // Issue #313
TEST_DOUBLE(fullPrecision, "-1e-00011111111111", -0.0); TEST_DOUBLE(fullPrecision, "-1e-00011111111111", -0.0);
TEST_DOUBLE(fullPrecision, "1e-429496729", 0.0); // Maximum supported negative exponent TEST_DOUBLE(fullPrecision, "1e-429496729", 0.0); // Maximum supported negative exponent
TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form
// Since // Since
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment