From 88ddacb84b75fe666ae8389dba2fd4f0f9661f75 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 7 Oct 2026 20:17:15 +0200 Subject: [PATCH] Fix CI warnings in own float parser - pow5_table.hpp: pow5_128_largest_power was unused in this branch's own code (GCC -Werror=unused-const-variable); tie it to the table size with a static_assert instead of removing it, since a later branch in the stack (json-view/23-zmij) uses it. - number_parse.hpp: rename the local variable `copy` to `buffer` to satisfy cpplint's build/include_what_you_use check. - unit-class_lexer.cpp: extend the NOLINT list on the seeded mt19937 with bugprone-random-generator-seed, and parenthesize `8 * sizeof(Bits) - 1` for clang-tidy. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/input/number_parse.hpp | 8 ++++---- include/nlohmann/detail/input/pow5_table.hpp | 4 ++++ single_include/nlohmann/json.hpp | 12 ++++++++---- tests/src/unit-class_lexer.cpp | 4 ++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/input/number_parse.hpp b/include/nlohmann/detail/input/number_parse.hpp index 45ad04a91..e12f5bcf7 100644 --- a/include/nlohmann/detail/input/number_parse.hpp +++ b/include/nlohmann/detail/input/number_parse.hpp @@ -987,10 +987,10 @@ void convert_float_locale_aware(StringType& token, std::size_t decimal_point_pos } else { - std::string copy(token.data(), token.size()); - copy.replace(decimal_point_position, 1, decimal_point); - strtof_by_type(value, copy.c_str(), &endptr); - complete = endptr == copy.c_str() + copy.size(); + std::string buffer(token.data(), token.size()); + buffer.replace(decimal_point_position, 1, decimal_point); + strtof_by_type(value, buffer.c_str(), &endptr); + complete = endptr == buffer.c_str() + buffer.size(); } if (JSON_HEDLEY_LIKELY(complete)) diff --git a/include/nlohmann/detail/input/pow5_table.hpp b/include/nlohmann/detail/input/pow5_table.hpp index 496fdb2f7..c7170e903 100644 --- a/include/nlohmann/detail/input/pow5_table.hpp +++ b/include/nlohmann/detail/input/pow5_table.hpp @@ -22,6 +22,10 @@ namespace detail constexpr std::int64_t pow5_128_smallest_power = -342; constexpr std::int64_t pow5_128_largest_power = 308; +// every entry of pow5_128() holds two 64-bit halves of 5^q, one per covered power of 5 +static_assert((pow5_128_largest_power - pow5_128_smallest_power + 1) * 2 == 1302, + "pow5_128_smallest_power/pow5_128_largest_power must match the size of the pow5_128() table"); + /*! @brief 128-bit approximations of 5^q for q in [-342, 308] diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ebf2b61f4..0b8192bf4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -8912,6 +8912,10 @@ namespace detail constexpr std::int64_t pow5_128_smallest_power = -342; constexpr std::int64_t pow5_128_largest_power = 308; +// every entry of pow5_128() holds two 64-bit halves of 5^q, one per covered power of 5 +static_assert((pow5_128_largest_power - pow5_128_smallest_power + 1) * 2 == 1302, + "pow5_128_smallest_power/pow5_128_largest_power must match the size of the pow5_128() table"); + /*! @brief 128-bit approximations of 5^q for q in [-342, 308] @@ -10225,10 +10229,10 @@ void convert_float_locale_aware(StringType& token, std::size_t decimal_point_pos } else { - std::string copy(token.data(), token.size()); - copy.replace(decimal_point_position, 1, decimal_point); - strtof_by_type(value, copy.c_str(), &endptr); - complete = endptr == copy.c_str() + copy.size(); + std::string buffer(token.data(), token.size()); + buffer.replace(decimal_point_position, 1, decimal_point); + strtof_by_type(value, buffer.c_str(), &endptr); + complete = endptr == buffer.c_str() + buffer.size(); } if (JSON_HEDLEY_LIKELY(complete)) diff --git a/tests/src/unit-class_lexer.cpp b/tests/src/unit-class_lexer.cpp index 632103839..3a842080c 100644 --- a/tests/src/unit-class_lexer.cpp +++ b/tests/src/unit-class_lexer.cpp @@ -801,7 +801,7 @@ TEST_CASE("lexer escape fast path") // digits and non-hex bytes, at varying distances from the start of // the string, to compare the two scanners on many more shapes than // are practical to enumerate by hand. - std::mt19937 gen(7654321); // NOLINT(cert-msc32-c,cert-msc51-cpp) + std::mt19937 gen(7654321); // NOLINT(cert-msc32-c,cert-msc51-cpp,bugprone-random-generator-seed) const std::string hex_alphabet = "0123456789AaBbCcDdEeFf"; std::uniform_int_distribution pick_hex(0, hex_alphabet.size() - 1); std::uniform_int_distribution pick_byte(1, 255); // never NUL @@ -1599,7 +1599,7 @@ template void check_parse(const std::string& token, Bits expected, Bits infinity) { std::stringstream stream(token); - if ((expected & ~(Bits{1} << (8 * sizeof(Bits) - 1))) == infinity) + if ((expected & ~(Bits{1} << ((8 * sizeof(Bits)) - 1))) == infinity) { Json _; CHECK_THROWS_WITH_AS(_ = Json::parse(token), ("[json.exception.out_of_range.406] number overflow parsing '" + token + "'").c_str(), typename Json::out_of_range&);