mirror of
https://github.com/nlohmann/json.git
synced 2026-08-22 09:03:18 +00:00
Bound the descent of the copy constructor
basic_json's copy constructor copied objects and arrays by handing the container to its own copy constructor, which copy-constructs every element and so reaches this constructor again, once per nesting level. A value nested deeply enough exhausted the call stack and terminated the process with a segmentation fault - no exception, nothing the caller could catch. Parsing such a value works, as the parser is iterative, and so does destroying one, as #1436 made destruction iterative. Bound how far the copy descends rather than take the call stack away from it. The first levels are copied exactly as they were - the containers copy their own elements, which is by far the fastest way to fill them - and only once the copy has descended 128 levels is the value below it finished without the call stack, through an explicit worklist. Copying can therefore no longer exhaust the stack, however deeply a value is nested, while a value nested less deeply than the bound - all but a vanishing minority - is copied by the very same code as before and pays only for one counter. That counter lives in thread_local storage, as one shared between threads would be raced. JSON_NO_THREAD_LOCAL switches it off for toolchains without thread_local; copying then goes through the worklist right away, which yields the same values but is measurably slower. The deferred values are completed before the copy they belong to returns, so a value copied while another copy is going on - by a custom base class, say - is unaffected by the copy it is nested in. operator= takes its argument by value, so copy assignment is fixed as well. Copying is as fast as it was, within measurement noise (medians of 9 interleaved runs, clang -O3): -1.3% for an array of strings, +0.0% for a flat object, +0.1% for a flat array of numbers, +0.3% for nested arrays, +0.6% for nested objects and +1.2% for a twitter-like document. Copying a three-key object costs about ten nanoseconds more, the counter. Deferring every level instead, rather than only those below the bound, measured between 3% and 9% slower depending on the shape of the value. This fixes #5387 for the copy constructor. dump() is still recursive. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
using nlohmann::json;
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("tests on very large JSONs")
|
||||
{
|
||||
@@ -27,3 +28,153 @@ TEST_CASE("tests on very large JSONs")
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// Descend a chain of single-element containers and return the value at its end,
|
||||
// reporting the number of levels traversed in @a depth.
|
||||
//
|
||||
// The values in the test case below are nested far deeper than the call stack
|
||||
// can follow, so they must not be inspected with operator== or dump(): both are
|
||||
// still recursive and would overflow the stack themselves.
|
||||
const json* innermost_value(const json& j, std::size_t& depth)
|
||||
{
|
||||
const json* current = &j;
|
||||
depth = 0;
|
||||
|
||||
while ((current->is_array() || current->is_object()) && !current->empty())
|
||||
{
|
||||
current = current->is_array()
|
||||
? ¤t->front()
|
||||
: ¤t->begin().value();
|
||||
++depth;
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("tests on deeply nested JSONs")
|
||||
{
|
||||
// deep enough to exhaust the call stack, but small enough to stay cheap:
|
||||
// parsing is iterative, so building the values below costs little
|
||||
const std::size_t depth = 100000;
|
||||
|
||||
SECTION("issue #5387 - stack overflow in the copy constructor")
|
||||
{
|
||||
SECTION("array")
|
||||
{
|
||||
const json j = json::parse(std::string(depth, '[') + '0' + std::string(depth, ']'));
|
||||
|
||||
const json copy(j);
|
||||
|
||||
std::size_t copy_depth = 0;
|
||||
CHECK(*innermost_value(copy, copy_depth) == 0);
|
||||
CHECK(copy_depth == depth);
|
||||
}
|
||||
|
||||
SECTION("object")
|
||||
{
|
||||
std::string s;
|
||||
s.reserve(6 * depth + 1);
|
||||
for (std::size_t i = 0; i < depth; ++i)
|
||||
{
|
||||
s += "{\"a\":";
|
||||
}
|
||||
s += '1';
|
||||
s.append(depth, '}');
|
||||
|
||||
const json j = json::parse(s);
|
||||
|
||||
const json copy(j);
|
||||
|
||||
std::size_t copy_depth = 0;
|
||||
CHECK(*innermost_value(copy, copy_depth) == 1);
|
||||
CHECK(copy_depth == depth);
|
||||
}
|
||||
|
||||
SECTION("copy assignment")
|
||||
{
|
||||
// operator=(basic_json) takes its argument by value, so the deep
|
||||
// copy happens in the copy constructor
|
||||
const json j = json::parse(std::string(depth, '[') + '0' + std::string(depth, ']'));
|
||||
|
||||
json target;
|
||||
target = j;
|
||||
|
||||
std::size_t target_depth = 0;
|
||||
CHECK(*innermost_value(target, target_depth) == 0);
|
||||
CHECK(target_depth == depth);
|
||||
}
|
||||
|
||||
SECTION("depths around the bound of the recursive descent")
|
||||
{
|
||||
// The copy constructor descends into a bounded number of levels and
|
||||
// completes whatever is below that without the call stack. Cover
|
||||
// every depth around that bound, so that the two ways of copying
|
||||
// are known to meet cleanly - wherever the bound is set.
|
||||
for (std::size_t d = 1; d <= 300; ++d)
|
||||
{
|
||||
CAPTURE(d);
|
||||
|
||||
const json array = json::parse(std::string(d, '[') + '0' + std::string(d, ']'));
|
||||
const json array_copy(array);
|
||||
std::size_t array_depth = 0;
|
||||
CHECK(*innermost_value(array_copy, array_depth) == 0);
|
||||
CHECK(array_depth == d);
|
||||
|
||||
std::string object_text;
|
||||
for (std::size_t i = 0; i < d; ++i)
|
||||
{
|
||||
object_text += "{\"a\":";
|
||||
}
|
||||
object_text += '1';
|
||||
object_text.append(d, '}');
|
||||
|
||||
const json object = json::parse(object_text);
|
||||
const json object_copy(object);
|
||||
std::size_t object_depth = 0;
|
||||
CHECK(*innermost_value(object_copy, object_depth) == 1);
|
||||
CHECK(object_depth == d);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("a value that is deep in one place only")
|
||||
{
|
||||
json j = json::object();
|
||||
j["shallow"] = 1;
|
||||
j["deep"] = json::parse(std::string(depth, '[') + '0' + std::string(depth, ']'));
|
||||
j["also_shallow"] = json::array({1, 2, 3});
|
||||
|
||||
const json copy(j);
|
||||
|
||||
CHECK(copy["shallow"] == 1);
|
||||
CHECK(copy["also_shallow"] == json::array({1, 2, 3}));
|
||||
|
||||
std::size_t deep_depth = 0;
|
||||
CHECK(*innermost_value(copy["deep"], deep_depth) == 0);
|
||||
CHECK(deep_depth == depth);
|
||||
}
|
||||
|
||||
SECTION("the copy is independent of the original")
|
||||
{
|
||||
const json j = json::parse(std::string(depth, '[') + '0' + std::string(depth, ']'));
|
||||
|
||||
json copy(j);
|
||||
|
||||
// reach the innermost value without recursing and replace it
|
||||
json* current = ©
|
||||
while (current->is_array() && !current->empty())
|
||||
{
|
||||
current = ¤t->front();
|
||||
}
|
||||
*current = 42;
|
||||
|
||||
std::size_t unused = 0;
|
||||
CHECK(*innermost_value(copy, unused) == 42);
|
||||
CHECK(*innermost_value(j, unused) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -81,3 +81,37 @@ TEST_CASE("regression test for issue #3732 - iteration_proxy_value<iter_impl<ord
|
||||
};
|
||||
static_cast<void>(fn);
|
||||
}
|
||||
|
||||
TEST_CASE("copying an ordered_json with nested values")
|
||||
{
|
||||
// ordered_map is backed by a vector, so copying an object that has
|
||||
// structured values takes a different route than copying a std::map-backed
|
||||
// one; see https://github.com/nlohmann/json/issues/5387
|
||||
ordered_json oj;
|
||||
oj["z"] = 1;
|
||||
oj["a"]["y"] = 2;
|
||||
oj["a"]["b"]["x"] = 3;
|
||||
oj["m"] = {1, 2, {{"w", 4}}};
|
||||
|
||||
const ordered_json copy(oj);
|
||||
|
||||
SECTION("the copy is equal to the original")
|
||||
{
|
||||
CHECK(copy == oj);
|
||||
CHECK(copy.dump() == oj.dump());
|
||||
}
|
||||
|
||||
SECTION("the key order is preserved at every level")
|
||||
{
|
||||
CHECK(copy.dump() == R"({"z":1,"a":{"y":2,"b":{"x":3}},"m":[1,2,{"w":4}]})");
|
||||
}
|
||||
|
||||
SECTION("the copy is independent of the original")
|
||||
{
|
||||
ordered_json mutated(oj);
|
||||
mutated["a"]["b"]["x"] = 99;
|
||||
|
||||
CHECK(oj["a"]["b"]["x"] == 3);
|
||||
CHECK(mutated["a"]["b"]["x"] == 99);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user