mirror of
https://github.com/nlohmann/json.git
synced 2026-09-01 05:57:14 +00:00
Assert memcpy buffer bounds in the contiguous get_elements fast path
Make the "buffers are large enough" reasoning explicit: before the copy, assert that `copied` fits both the caller-provided destination (`wanted` bytes) and the remaining input range (`available` bytes). These hold by construction (`copied = min(wanted, available)`) but were previously only implied; the asserts document the invariant and would catch a future regression that breaks it. Signed-off-by: Niels Lohmann <mail@nlohmann.me> Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
5355190cff
commit
79a8c279c0
@@ -206,6 +206,11 @@ class iterator_input_adapter
|
||||
const std::size_t copied = (std::min)(wanted, available);
|
||||
if (JSON_HEDLEY_LIKELY(copied != 0))
|
||||
{
|
||||
// the copy must stay within both buffers: the caller-provided
|
||||
// destination holds `wanted` bytes and the remaining input range
|
||||
// holds `available` bytes, and `copied` is the minimum of the two
|
||||
JSON_ASSERT(copied <= wanted); // does not overrun the destination
|
||||
JSON_ASSERT(copied <= available); // does not read past the input end
|
||||
// &*current yields the raw address for both raw pointers and
|
||||
// non-pointer contiguous iterators (e.g. std::vector's iterator)
|
||||
std::memcpy(dest, &*current, copied);
|
||||
|
||||
@@ -7038,6 +7038,11 @@ class iterator_input_adapter
|
||||
const std::size_t copied = (std::min)(wanted, available);
|
||||
if (JSON_HEDLEY_LIKELY(copied != 0))
|
||||
{
|
||||
// the copy must stay within both buffers: the caller-provided
|
||||
// destination holds `wanted` bytes and the remaining input range
|
||||
// holds `available` bytes, and `copied` is the minimum of the two
|
||||
JSON_ASSERT(copied <= wanted); // does not overrun the destination
|
||||
JSON_ASSERT(copied <= available); // does not read past the input end
|
||||
// &*current yields the raw address for both raw pointers and
|
||||
// non-pointer contiguous iterators (e.g. std::vector's iterator)
|
||||
std::memcpy(dest, &*current, copied);
|
||||
|
||||
Reference in New Issue
Block a user