diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 754a484a5..81a3ebd42 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -4940,12 +4940,15 @@ class binary_reader { return false; } + // (len > 0, so the unsigned type holds it; casting a signed char + // straight to std::size_t would trip bugprone-signed-char-misuse) + const auto ulen = static_cast::type>(len); const std::size_t remaining = ia.bulk_remaining(); - if (static_cast(len) > static_cast(remaining)) + if (static_cast(ulen) > static_cast(remaining)) { return false; // (the loop reports the premature end) } - const auto n = static_cast(len); + const auto n = static_cast(ulen); result.append(reinterpret_cast(ia.bulk_data()), n); ia.bulk_skip(n); chars_read += n; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e171ec85d..6e2ead1b4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22956,12 +22956,15 @@ class binary_reader { return false; } + // (len > 0, so the unsigned type holds it; casting a signed char + // straight to std::size_t would trip bugprone-signed-char-misuse) + const auto ulen = static_cast::type>(len); const std::size_t remaining = ia.bulk_remaining(); - if (static_cast(len) > static_cast(remaining)) + if (static_cast(ulen) > static_cast(remaining)) { return false; // (the loop reports the premature end) } - const auto n = static_cast(len); + const auto n = static_cast(ulen); result.append(reinterpret_cast(ia.bulk_data()), n); ia.bulk_skip(n); chars_read += n;