Share one recursion depth limit, and copy the hash frame out of the stack

dump() and hash() each defined their own limit on how many nesting levels
they recurse into, and the operations still to come would have added more,
free to diverge over time. They now all use detail::recursion_depth_limit(),
in a header of its own; serializer::dump_depth_limit() and
hash_depth_limit() are gone.

hash_iteratively() now copies the frame it works on out of the stack and
changes the frame only through stack.back(), so nothing can refer into
the stack after entering an element has grown it.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-09-23 23:07:11 +02:00
1 parent 75c61f91f1
commit 57a5a5d48e
7 files changed
+113 -58

No files matched your search

+12 -17
View File
@@ -14,6 +14,7 @@
#include <vector> // vector
#include <nlohmann/detail/abi_macros.hpp>
#include <nlohmann/detail/recursion_depth_limit.hpp>
#include <nlohmann/detail/value_t.hpp>
NLOHMANN_JSON_NAMESPACE_BEGIN
@@ -27,13 +28,6 @@ inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
return seed;
}
/// the number of levels @ref hash descends into before handing over to
/// @ref hash_iteratively
constexpr std::size_t hash_depth_limit() noexcept
{
return 128;
}
template<typename BasicJsonType>
std::size_t hash_iteratively(const BasicJsonType& j);
@@ -47,7 +41,7 @@ null, 0, 0U, and false, etc.
Hashing an array or an object hashes its elements, which used to call this
function again once per nesting level, so a value nested deeply enough
exhausted the call stack and terminated the process. The descent is bounded
here: once @ref hash_depth_limit levels have been entered, @ref
here: once @ref recursion_depth_limit levels have been entered, @ref
hash_iteratively hashes what is left without the call stack. A value nested
less deeply than that - all but a vanishing minority - is hashed exactly as
before, without allocating.
@@ -76,7 +70,7 @@ std::size_t hash(const BasicJsonType& j, const std::size_t depth = 0)
case BasicJsonType::value_t::object:
{
if (JSON_HEDLEY_UNLIKELY(depth >= hash_depth_limit()))
if (JSON_HEDLEY_UNLIKELY(depth >= recursion_depth_limit()))
{
return hash_iteratively(j);
}
@@ -93,7 +87,7 @@ std::size_t hash(const BasicJsonType& j, const std::size_t depth = 0)
case BasicJsonType::value_t::array:
{
if (JSON_HEDLEY_UNLIKELY(depth >= hash_depth_limit()))
if (JSON_HEDLEY_UNLIKELY(depth >= recursion_depth_limit()))
{
return hash_iteratively(j);
}
@@ -175,7 +169,7 @@ struct hash_frame
Computes the same value as @ref hash, keeping the arrays and objects it has
entered on an explicit stack instead of descending into them. Only reached for
values nested deeper than @ref hash_depth_limit.
values nested deeper than @ref recursion_depth_limit.
@tparam BasicJsonType basic_json specialization
@param j array or object to hash
@@ -191,7 +185,9 @@ std::size_t hash_iteratively(const BasicJsonType& j)
while (true)
{
hash_frame<BasicJsonType>& frame = stack.back();
// a copy, as entering an element below can reallocate the stack; the
// frame itself is only changed through stack.back()
const hash_frame<BasicJsonType> frame = stack.back();
if (frame.position == frame.value->cend())
{
@@ -209,13 +205,12 @@ std::size_t hash_iteratively(const BasicJsonType& j)
if (frame.value->is_object())
{
frame.seed = combine(frame.seed, std::hash<string_t> {}(frame.position.key()));
stack.back().seed = combine(stack.back().seed, std::hash<string_t> {}(frame.position.key()));
}
// read the element and advance before entering it: entering can
// reallocate the stack and so invalidate `frame`
// advance before entering the element, which pushes onto the stack
const BasicJsonType& element = *frame.position;
++frame.position;
++stack.back().position;
if (element.is_structured())
{
@@ -223,7 +218,7 @@ std::size_t hash_iteratively(const BasicJsonType& j)
}
else
{
frame.seed = combine(frame.seed, hash(element));
stack.back().seed = combine(stack.back().seed, hash(element));
}
}
}