Fix bug when parsing NaN, Inf with fraction or exponent parts (fixes #2299)

This patch fixes the issue where parsing NaN or Inf values with
fractional or exponent parts would return incorrect results
(e.g., "NaN.2e2" would be parsed as 20).
Before this patch, the parser would continue to process the fractional
and exponent parts even after successfully parsing a valid NaN or Inf,
which could lead to parsing errors. This patch adds a check for such
cases to skips the parsing of the fractional and exponent parts after
completing the NaN and Inf parsing.
This commit is contained in:
Eyizoha 2024-08-15 18:27:23 +08:00 committed by Milo Yip
parent ab1842a2da
commit 7c73dd7de7
2 changed files with 5 additions and 2 deletions

View File

@ -1584,7 +1584,7 @@ private:
// Parse frac = decimal-point 1*DIGIT // Parse frac = decimal-point 1*DIGIT
int expFrac = 0; int expFrac = 0;
size_t decimalPosition; size_t decimalPosition;
if (Consume(s, '.')) { if (!useNanOrInf && Consume(s, '.')) {
decimalPosition = s.Length(); decimalPosition = s.Length();
if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9'))) if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9')))
@ -1631,7 +1631,7 @@ private:
// Parse exp = e [ minus / plus ] 1*DIGIT // Parse exp = e [ minus / plus ] 1*DIGIT
int exp = 0; int exp = 0;
if (Consume(s, 'e') || Consume(s, 'E')) { if (!useNanOrInf && (Consume(s, 'e') || Consume(s, 'E'))) {
if (!useDouble) { if (!useDouble) {
d = static_cast<double>(use64bit ? i64 : i); d = static_cast<double>(use64bit ? i64 : i);
useDouble = true; useDouble = true;

View File

@ -2338,6 +2338,9 @@ TEST(Reader, ParseNanAndInfinity) {
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1u); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1u); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1u);
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6u); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6u);
TEST_NAN_INF_ERROR(kParseErrorDocumentRootNotSingular, "NaN.2e2", 3u);
TEST_NAN_INF_ERROR(kParseErrorDocumentRootNotSingular, "Inf.2", 3u);
TEST_NAN_INF_ERROR(kParseErrorDocumentRootNotSingular, "-InfE2", 4u);
#undef TEST_NAN_INF_ERROR #undef TEST_NAN_INF_ERROR
#undef TEST_NAN_INF #undef TEST_NAN_INF