From e36340da398eefce17fec5acf173dad659bd6040 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 5 Jul 2026 10:42:28 +0200 Subject: [PATCH] Tighten resize() postcondition assert from >= to == std::vector::resize(n) (and string_t's resize) is required to make size() exactly n; only capacity() is permitted to overshoot. The destination-size assert in get_bytes should reflect that exact guarantee rather than a weaker `>=`. Signed-off-by: Niels Lohmann Co-Authored-By: Claude Opus 4.8 --- include/nlohmann/detail/input/binary_reader.hpp | 6 +++--- single_include/nlohmann/json.hpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 205db484b..c183a94fb 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -2970,9 +2970,9 @@ class binary_reader : chunk_size; const std::size_t old_size = result.size(); result.resize(old_size + wanted); - // the destination now has room for `wanted` bytes at offset - // old_size, which is what get_elements() is allowed to write - JSON_ASSERT(result.size() >= old_size + wanted); + // resize() is required to make size() exactly old_size + wanted; + // that is the room get_elements() is allowed to write into + JSON_ASSERT(result.size() == old_size + wanted); const std::size_t bytes_read = ia.get_elements(&result[old_size], wanted); chars_read += bytes_read; if (JSON_HEDLEY_UNLIKELY(bytes_read < wanted)) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index dea47a62d..443574a00 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -13176,9 +13176,9 @@ class binary_reader : chunk_size; const std::size_t old_size = result.size(); result.resize(old_size + wanted); - // the destination now has room for `wanted` bytes at offset - // old_size, which is what get_elements() is allowed to write - JSON_ASSERT(result.size() >= old_size + wanted); + // resize() is required to make size() exactly old_size + wanted; + // that is the room get_elements() is allowed to write into + JSON_ASSERT(result.size() == old_size + wanted); const std::size_t bytes_read = ia.get_elements(&result[old_size], wanted); chars_read += bytes_read; if (JSON_HEDLEY_UNLIKELY(bytes_read < wanted))