From 759a847178c419e2ea5d2833896481211c3a9d60 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 6 Nov 2026 12:13:09 +0100 Subject: [PATCH] Fix CI: bugprone-signed-char-misuse in append_from_block Convert the (positive) length to its unsigned type before widening it to std::size_t, as clang-tidy flags the int8_t to std::size_t cast. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/binary_reader.hpp | 7 +++++-- single_include/nlohmann/json.hpp | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) 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;