diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 39af2c4c1..062142d7c 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -49,6 +49,36 @@ namespace Json { using CharReaderPtr = std::unique_ptr; +// Returns true when [begin, end) is a well-formed JSON number token. Used by +// decodeDouble() to distinguish floating-point overflow from malformed tokens +// like "0e" or "0e+". +bool isWellFormedJsonNumber(const char* begin, const char* end) { + const char* p = begin; + if (p != end && *p == '-') + ++p; + if (p == end || *p < '0' || *p > '9') + return false; + while (p != end && *p >= '0' && *p <= '9') + ++p; + if (p != end && *p == '.') { + ++p; + if (p == end || *p < '0' || *p > '9') + return false; + while (p != end && *p >= '0' && *p <= '9') + ++p; + } + if (p != end && (*p == 'e' || *p == 'E')) { + ++p; + if (p != end && (*p == '+' || *p == '-')) + ++p; + if (p == end || *p < '0' || *p > '9') + return false; + while (p != end && *p >= '0' && *p <= '9') + ++p; + } + return p == end; +} + // Implementation of class Features // //////////////////////////////// @@ -587,12 +617,22 @@ bool Reader::decodeDouble(Token& token, Value& decoded) { // operator>> sets failbit for a subnormal result (underflow) even though // it produced the correctly-rounded value, which made such numbers fail to // parse back after jsoncpp serialized them. Keep a subnormal value instead - // of rejecting it. See issue #1427. Other failures -- malformed numbers - // like "0e" or "0e+", or non-numbers -- leave the value at zero/non-finite - // and are still rejected. - else if (!std::isinf(value) && std::fpclassify(value) != FP_SUBNORMAL) + // of rejecting it. See issue #1427. + else if (std::fpclassify(value) == FP_SUBNORMAL) { + // keep the correctly-rounded subnormal value + } + // Some standard libraries (e.g. MSVC) set failbit on overflow without + // writing the max()/lowest() sentinels, leaving `value` untouched. If the + // token is a well-formed number, a failed conversion means it is out of + // range: map it to +/-infinity so parsing behaves identically everywhere + // (see issues #1446 and #1405). + else if (isWellFormedJsonNumber(token.start_, token.end_)) { + value = *token.start_ == '-' ? -std::numeric_limits::infinity() + : std::numeric_limits::infinity(); + } else { return addError( "'" + String(token.start_, token.end_) + "' is not a number.", token); + } } decoded = value; return true; @@ -1678,12 +1718,22 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) { // operator>> sets failbit for a subnormal result (underflow) even though // it produced the correctly-rounded value, which made such numbers fail to // parse back after jsoncpp serialized them. Keep a subnormal value instead - // of rejecting it. See issue #1427. Other failures -- malformed numbers - // like "0e" or "0e+", or non-numbers -- leave the value at zero/non-finite - // and are still rejected. - else if (!std::isinf(value) && std::fpclassify(value) != FP_SUBNORMAL) + // of rejecting it. See issue #1427. + else if (std::fpclassify(value) == FP_SUBNORMAL) { + // keep the correctly-rounded subnormal value + } + // Some standard libraries (e.g. MSVC) set failbit on overflow without + // writing the max()/lowest() sentinels, leaving `value` untouched. If the + // token is a well-formed number, a failed conversion means it is out of + // range: map it to +/-infinity so parsing behaves identically everywhere + // (see issues #1446 and #1405). + else if (isWellFormedJsonNumber(token.start_, token.end_)) { + value = *token.start_ == '-' ? -std::numeric_limits::infinity() + : std::numeric_limits::infinity(); + } else { return addError( "'" + String(token.start_, token.end_) + "' is not a number.", token); + } } decoded = value; return true; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 313e9f947..511d98f94 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3198,6 +3198,25 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, parseSpecialFloat) { "expected.\n"); } +JSONTEST_FIXTURE_LOCAL(ReaderTest, parseOverflow) { + // The legacy Reader has the same overflow handling as CharReader; + // out-of-range numbers must parse to +/-infinity (see #1446, #1405). + { + std::string doc = "[-1e+9999, 1e+9999]"; + JSONTEST_ASSERT(reader->parse(doc, root)); + JSONTEST_ASSERT_EQUAL(root.size(), 2u); + JSONTEST_ASSERT(std::isinf(root[0].asDouble())); + JSONTEST_ASSERT(root[0].asDouble() < 0); + JSONTEST_ASSERT(std::isinf(root[1].asDouble())); + JSONTEST_ASSERT(root[1].asDouble() > 0); + } + { + // Malformed numbers are still rejected. + std::string doc = "[0e]"; + JSONTEST_ASSERT(!reader->parse(doc, root)); + } +} + JSONTEST_FIXTURE_LOCAL(ReaderTest, strictModeParseNumber) { setStrictMode(); checkParse( @@ -3349,6 +3368,67 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseSubnormal) { } } +JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseOverflow) { + // Regression test for #1446/#1405: out-of-range numbers must parse to + // +/-infinity on every platform. Some standard libraries (e.g. MSVC) report + // overflow as a failed extraction without writing the max()/lowest() + // sentinels, which used to make tokens like "-1e+9999" fail to parse. + Json::CharReaderBuilder b; + CharReaderPtr reader(b.newCharReader()); + Json::String errs; + + const struct { + const char* doc; + double expected; + } cases[] = { + {"[1e+9999]", std::numeric_limits::infinity()}, + {"[-1e+9999]", -std::numeric_limits::infinity()}, + {"[1e309]", std::numeric_limits::infinity()}, + {"[-1e309]", -std::numeric_limits::infinity()}, + // An integer too large for any integral or floating-point type maps to + // infinity as well. + {"[9e99999999999999999999999999999999999999999999999999999999999999]", + std::numeric_limits::infinity()}, + }; + for (const auto& c : cases) { + Json::Value root; + bool ok = reader->parse(c.doc, c.doc + std::strlen(c.doc), &root, &errs); + JSONTEST_ASSERT(ok); + JSONTEST_ASSERT(errs.empty()); + JSONTEST_ASSERT(std::isinf(root[0].asDouble())); + JSONTEST_ASSERT_EQUAL(c.expected, root[0].asDouble()); + } + + // The exact shape of test/data/legacy_test_real_13.json. + { + Json::Value root; + char const doc[] = "[-1e+9999, 1e+9999]"; + bool ok = reader->parse(doc, doc + sizeof(doc) - 1, &root, &errs); + JSONTEST_ASSERT(ok); + JSONTEST_ASSERT_EQUAL(root.size(), 2u); + JSONTEST_ASSERT(std::isinf(root[0].asDouble())); + JSONTEST_ASSERT(root[0].asDouble() < 0); + JSONTEST_ASSERT(std::isinf(root[1].asDouble())); + JSONTEST_ASSERT(root[1].asDouble() > 0); + } + + // Infinity written by the default writer (useSpecialFloats=false) + // round-trips. + { + Json::Value v(Json::arrayValue); + v[0] = std::numeric_limits::infinity(); + v[1] = -std::numeric_limits::infinity(); + const Json::String doc = Json::writeString(Json::StreamWriterBuilder(), v); + Json::Value root; + bool ok = reader->parse(doc.data(), doc.data() + doc.size(), &root, &errs); + JSONTEST_ASSERT(ok); + JSONTEST_ASSERT(std::isinf(root[0].asDouble())); + JSONTEST_ASSERT(root[0].asDouble() > 0); + JSONTEST_ASSERT(std::isinf(root[1].asDouble())); + JSONTEST_ASSERT(root[1].asDouble() < 0); + } +} + JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) { Json::CharReaderBuilder b; CharReaderPtr reader(b.newCharReader());