Skip to content

Commit 0eaa0d2

Browse files
authored
Merge pull request Tencent#862 from StilesCrisis/nan-inf-parse-fix
NaN/Inf parse fix
2 parents 0163a53 + 5e785d3 commit 0eaa0d2

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

include/rapidjson/reader.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,18 +1170,27 @@ class GenericReader {
11701170
}
11711171
// Parse NaN or Infinity here
11721172
else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) {
1173-
useNanOrInf = true;
1174-
if (RAPIDJSON_LIKELY(Consume(s, 'N') && Consume(s, 'a') && Consume(s, 'N'))) {
1175-
d = std::numeric_limits<double>::quiet_NaN();
1173+
if (Consume(s, 'N')) {
1174+
if (Consume(s, 'a') && Consume(s, 'N')) {
1175+
d = std::numeric_limits<double>::quiet_NaN();
1176+
useNanOrInf = true;
1177+
}
11761178
}
1177-
else if (RAPIDJSON_LIKELY(Consume(s, 'I') && Consume(s, 'n') && Consume(s, 'f'))) {
1178-
d = (minus ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
1179-
if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n')
1180-
&& Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y'))))
1181-
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());
1179+
else if (RAPIDJSON_LIKELY(Consume(s, 'I'))) {
1180+
if (Consume(s, 'n') && Consume(s, 'f')) {
1181+
d = (minus ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity());
1182+
useNanOrInf = true;
1183+
1184+
if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n')
1185+
&& Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) {
1186+
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());
1187+
}
1188+
}
11821189
}
1183-
else
1190+
1191+
if (RAPIDJSON_UNLIKELY(!useNanOrInf)) {
11841192
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());
1193+
}
11851194
}
11861195
else
11871196
RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell());

test/unittest/readertest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,10 @@ TEST(Reader, ParseNanAndInfinity) {
18321832
TEST_NAN_INF("Infinity", inf);
18331833
TEST_NAN_INF("-Inf", -inf);
18341834
TEST_NAN_INF("-Infinity", -inf);
1835+
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NInf", 1);
1836+
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 2);
1837+
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "INan", 1);
1838+
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 2);
18351839
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1);
18361840
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1);
18371841
TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1);

0 commit comments

Comments
 (0)