Repository navigation
fix: reject string values and object keys too long to store #1733
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 |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ | |
| #include "jsontest.h" | ||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <functional> | ||
| #include <iomanip> | ||
|
|
@@ -2184,6 +2186,37 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, removeMemberWithEmbeddedNull) { | |
| } | ||
| } | ||
|
|
||
| JSONTEST_FIXTURE_LOCAL(ValueTest, lengthsTooLongToStoreAreNotTruncated) { | ||
| #if JSON_USE_EXCEPTION | ||
| // Lengths are checked before anything is read, so most of this buffer stays | ||
| // untouched and costs address space, not memory. | ||
| const std::uint64_t stringLength = (std::uint64_t{1} << 32) + 1; | ||
| const size_t keyLength = (size_t{1} << 30) + 1; | ||
| if (stringLength > std::numeric_limits<size_t>::max()) | ||
| return; | ||
| std::unique_ptr<char, decltype(&std::free)> buffer( | ||
| static_cast<char*>(std::malloc(static_cast<size_t>(stringLength))), | ||
|
Comment on lines
+2195
to
+2198
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 early return for Split the string and key checks so each can run independently. |
||
| &std::free); | ||
| if (!buffer) | ||
| return; | ||
| buffer.get()[0] = 'x'; | ||
| const char* begin = buffer.get(); | ||
|
|
||
| JSONTEST_ASSERT_THROWS( | ||
| Json::Value(begin, begin + static_cast<size_t>(stringLength))); | ||
|
|
||
| Json::Value root(Json::objectValue); | ||
| JSONTEST_ASSERT_THROWS(root.demand(begin, begin + keyLength)); | ||
| JSONTEST_ASSERT(root.empty()); | ||
|
|
||
| // Must not alias the key made of the low bits of the length. | ||
| root["x"] = "keep"; | ||
| JSONTEST_ASSERT(root.find(begin, begin + keyLength) == nullptr); | ||
| JSONTEST_ASSERT(!root.removeMember(begin, begin + keyLength, nullptr)); | ||
| JSONTEST_ASSERT_EQUAL(1U, root.size()); | ||
| #endif | ||
| } | ||
|
|
||
| JSONTEST_FIXTURE_LOCAL(ValueTest, removeMemberStringOnNullAndEmptyKey) { | ||
| Json::Value root; | ||
| root.removeMember("missing"); | ||
|
|
||
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.
The new check misses
removeMember(const char*). That overload still builds aCZStringdirectly fromstrlen(key). For a null-terminated key of length2^30 + 1starting with"x",CZStringstores length 1, soroot.removeMember(key)can deleteroot["x"].Route that overload through the checked overload while keeping its existing type check.