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

This commit is contained in:
Milo Yip 2015-04-24 22:50:42 +08:00
parent 93d13ad2ac
commit 735354efd3
2 changed files with 16 additions and 8 deletions

View File

@ -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());
} }
} }
} }

View File

@ -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