From 4a4aa8cfc0253e466f6b66d03540f0e96bbe7503 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 10 Oct 2026 21:37:48 +0200 Subject: [PATCH] Make max_input_size a function (GCC -Wunused-const-variable) Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/errors.hpp | 2 +- include/nlohmann/detail/view/node.hpp | 5 ++++- include/nlohmann/json_view.hpp | 2 +- tests/src/unit-json_view.cpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/view/errors.hpp b/include/nlohmann/detail/view/errors.hpp index 8d67a3973..06c97b552 100644 --- a/include/nlohmann/detail/view/errors.hpp +++ b/include/nlohmann/detail/view/errors.hpp @@ -55,7 +55,7 @@ template { if (f.code == error_code::input_too_large) { - // (the limit is detail::view::max_input_size: 4 GiB minus 16 bytes) + // (the limit is detail::view::max_input_size(): 4 GiB minus 16 bytes) NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4294967280 bytes or more is not supported by json_document", nullptr)); } const BasicJsonType accepted = BasicJsonType::parse(src, src + size, nullptr, true, ignore_comments, ignore_trailing_commas); diff --git a/include/nlohmann/detail/view/node.hpp b/include/nlohmann/detail/view/node.hpp index b36fc8c2a..891424281 100644 --- a/include/nlohmann/detail/view/node.hpp +++ b/include/nlohmann/detail/view/node.hpp @@ -32,7 +32,10 @@ static_assert(static_cast(value_t::null) == 0 && static_cast detail::view::max_input_size)) + if (NLOHMANN_VIEW_UNLIKELY(size > detail::view::max_input_size())) { failure.code = detail::view::error_code::input_too_large; } diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index e9cc4ca5f..b92dcae1a 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -457,7 +457,7 @@ TEST_CASE("json_view") { // 32-bit offsets: the limit is 4 GiB minus 16 bytes (a margin below 2^32), // which is what the exception message and the documentation say - const std::size_t limit = nlohmann::detail::view::max_input_size; + const std::size_t limit = nlohmann::detail::view::max_input_size(); CHECK(limit == std::size_t{4294967279u}); const oversized_input input{limit + 1};