mirror of
https://github.com/nlohmann/json.git
synced 2026-09-01 14:07:14 +00:00
Fix GCC narrowing warning and address review feedback
- get_bytes(): replace `len -= static_cast<NumberType>(wanted)` with an explicit `len = static_cast<NumberType>(len - ...)`. The compound assignment promoted the operands to int and narrowed back, which GCC rejects under -Werror=arith-conversion when NumberType is narrower than int (e.g. UBJSON's int16 length). This also fixes the CodeQL build. - get_bytes(): add JSON_ASSERT(read == wanted) documenting that get_elements() never returns more than requested (per review). - iterator_input_adapter: de-duplicate the iterator_is_contiguous definition with the #if inside the initializer (per review). - Add a comment explaining why the memcpy source is &*current (needed for non-pointer contiguous iterators), and drop the redundant parentheses. - Run astyle on the new unit test so the amalgamation/format check passes. 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
9eee7a7b9b
commit
282ff6ce5c
@@ -7023,11 +7023,11 @@ class iterator_input_adapter
|
||||
// whether IteratorType refers to a contiguous range and therefore supports
|
||||
// a std::memcpy fast path (pointers always do; in C++20 we can also detect
|
||||
// library iterators such as those of std::vector and std::string)
|
||||
static constexpr bool iterator_is_contiguous =
|
||||
#if defined(__cpp_lib_concepts) && defined(JSON_HAS_CPP_20)
|
||||
static constexpr bool iterator_is_contiguous = std::is_pointer<IteratorType>::value || std::contiguous_iterator<IteratorType>;
|
||||
#else
|
||||
static constexpr bool iterator_is_contiguous = std::is_pointer<IteratorType>::value;
|
||||
std::contiguous_iterator<IteratorType> ||
|
||||
#endif
|
||||
std::is_pointer<IteratorType>::value;
|
||||
|
||||
// contiguous fast path: bulk copy the remaining range with std::memcpy
|
||||
template<class T>
|
||||
@@ -7038,7 +7038,9 @@ class iterator_input_adapter
|
||||
const std::size_t copied = (std::min)(wanted, available);
|
||||
if (JSON_HEDLEY_LIKELY(copied != 0))
|
||||
{
|
||||
std::memcpy(dest, &(*current), copied);
|
||||
// &*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);
|
||||
std::advance(current, static_cast<typename std::iterator_traits<IteratorType>::difference_type>(copied / sizeof(char_type)));
|
||||
}
|
||||
return copied;
|
||||
@@ -13174,13 +13176,16 @@ class binary_reader
|
||||
if (JSON_HEDLEY_UNLIKELY(read < wanted))
|
||||
{
|
||||
// premature end of input: shrink to what was actually read and
|
||||
// report the failure at the first missing byte
|
||||
// report the failure at the first missing byte (same position
|
||||
// accounting as get_to() for partial number reads)
|
||||
result.resize(old_size + read);
|
||||
++chars_read;
|
||||
current = char_traits<char_type>::eof();
|
||||
return unexpect_eof(format, context);
|
||||
}
|
||||
len -= static_cast<NumberType>(wanted);
|
||||
// a full chunk was read; get_elements() never returns more than requested
|
||||
JSON_ASSERT(read == wanted);
|
||||
len = static_cast<NumberType>(len - static_cast<NumberType>(wanted));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user