Repository navigation
Parse out-of-range floats as infinity on all platforms #1731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<double>::infinity()}, | ||
| {"[-1e+9999]", -std::numeric_limits<double>::infinity()}, | ||
| {"[1e309]", std::numeric_limits<double>::infinity()}, | ||
| {"[-1e309]", -std::numeric_limits<double>::infinity()}, | ||
| // An integer too large for any integral or floating-point type maps to | ||
| // infinity as well. | ||
| {"[9e99999999999999999999999999999999999999999999999999999999999999]", | ||
| std::numeric_limits<double>::infinity()}, | ||
|
Comment on lines
+3388
to
+3391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The case described as an oversized integer uses Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| }; | ||
| 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<double>::infinity(); | ||
| v[1] = -std::numeric_limits<double>::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()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the standard library sets
failbitand returns zero for a tiny number such as1e-9999, zero skips theFP_SUBNORMALbranch.isWellFormedJsonNumberthen makes the reader report success with infinity instead of zero or a parse error. BothdecodeDoubleimplementations have this problem. Distinguish overflow from underflow before assigning infinity, and add positive and negative underflow tests.Knowledge Base Used: Parser internals