You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
contains(json_pointer) throws on out-of-range numeric array tokens, contradicting its documented "does not throw exceptions" #5395
However, when the pointer contains an array-index token that consists only of digits but is numerically too large for size_type (or exceeds ULLONG_MAX, making strtoull set ERANGE), contains() throws.
contains() was deliberately written to return false for malformed reference tokens ("00", "one", "+1", …) instead of throwing. The check at include/nlohmann/detail/json_pointer.hpp:751 validates only the characters of the token, not its magnitude, and then calls array_index, which throws out_of_range.404 / out_of_range.410 (throw sites at json_pointer.hpp:276 and :283).
This means a documented-noexcept-in-spirit API throws on attacker-controllable pointer strings. In a noexcept context this leads to std::terminate.
Reproduction steps
Build and run the minimal example below against an array (or any value) with a JSON pointer whose token is all digits and ≥ the maximum representable index.
Expected vs. actual results
Expected:contains() returns false (the reference token cannot resolve), consistent with the documentation and with its handling of other malformed tokens.
Actual: it throws out_of_range.410 (index exceeds size_type) or out_of_range.404 (unresolved reference token for values above ULLONG_MAX).
Minimal code example
#include<nlohmann/json.hpp>using json = nlohmann::json;
intmain()
{
json arr = json::array({1, 2, 3});
// token is all digits but larger than size_type max:
arr.contains(json::json_pointer("/18446744073709551615")); // throws out_of_range.410// token larger than ULLONG_MAX (ERANGE):
arr.contains(json::json_pointer("/99999999999999999999")); // throws out_of_range.404
}
Error messages
terminate called after throwing an instance of 'nlohmann::json_abi_v3_12_0::detail::out_of_range'
what(): [json.exception.out_of_range.410] array index 18446744073709551615 exceeds size_type
(For the ULLONG_MAX+ case: [json.exception.out_of_range.404] unresolved reference token '99999999999999999999'.)
Suggested fix
In contains(), treat a numeric token that does not fit size_type the same way malformed tokens are already treated — return false rather than delegating to array_index. The existing unit tests cover this range check only for operator[]/at (unit-json_pointer.cpp:316), never for contains.
Activity
Make contains(json_pointer) return false instead of throwing on unrep…
Unify json_pointer's three array-index parsers
Do not throw in contains() for an empty array reference token (#5614)
Fix lint debt: enum-macro NOLINTs, doctest as SYSTEM, no-op analyzer (#…
Remove dead metaprogramming and duplicated code in traits and pointers (