diff --git a/cmake/requirements/requirements-cppcheck.txt b/cmake/requirements/requirements-cppcheck.txt index 9119ad7b6..aa1591128 100644 --- a/cmake/requirements/requirements-cppcheck.txt +++ b/cmake/requirements/requirements-cppcheck.txt @@ -1 +1 @@ -cppcheck==1.5.0 +cppcheck==1.5.1 diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index aeec34eeb..c1d50740a 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -825,14 +825,27 @@ class serializer o->write_characters(begin, static_cast(end - begin)); } + JSON_HEDLEY_NON_NULL(1) + static int snprintf_float(char* buf, std::size_t size, int d, double x) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) + return (std::snprintf)(buf, size, "%.*g", d, x); + } + + JSON_HEDLEY_NON_NULL(1) + static int snprintf_float(char* buf, std::size_t size, int d, long double x) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) + return (std::snprintf)(buf, size, "%.*lg", d, x); + } + void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/) { // get the number of digits for a float -> text -> float round-trip static constexpr auto d = std::numeric_limits::max_digits10; // the actual conversion - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); + std::ptrdiff_t len = snprintf_float(number_buffer.data(), number_buffer.size(), d, x); // negative value indicates an error JSON_ASSERT(len > 0); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 69073a496..65b3b3459 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -19807,14 +19807,27 @@ class serializer o->write_characters(begin, static_cast(end - begin)); } + JSON_HEDLEY_NON_NULL(1) + static int snprintf_float(char* buf, std::size_t size, int d, double x) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) + return (std::snprintf)(buf, size, "%.*g", d, x); + } + + JSON_HEDLEY_NON_NULL(1) + static int snprintf_float(char* buf, std::size_t size, int d, long double x) + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) + return (std::snprintf)(buf, size, "%.*lg", d, x); + } + void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/) { // get the number of digits for a float -> text -> float round-trip static constexpr auto d = std::numeric_limits::max_digits10; // the actual conversion - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); + std::ptrdiff_t len = snprintf_float(number_buffer.data(), number_buffer.size(), d, x); // negative value indicates an error JSON_ASSERT(len > 0); diff --git a/tests/src/unit-ordered_json.cpp b/tests/src/unit-ordered_json.cpp index 63938e60b..a38a1a2b8 100644 --- a/tests/src/unit-ordered_json.cpp +++ b/tests/src/unit-ordered_json.cpp @@ -75,7 +75,7 @@ TEST_CASE("regression test for issue #3732 - iteration_proxy_value; previously this hit an // incomplete-type error in set_parents(). - auto fn = [](nlohmann::detail::iteration_proxy_value> const& val) + auto fn = [](nlohmann::detail::iteration_proxy_value> const & val) { return val.value(); }; diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 959e30197..f60b4a277 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -1275,8 +1275,8 @@ TEST_CASE("regression test #5122 - from_json into types holding nlohmann::ordere // because GCC does not provide it and would tokenize-error on the argument. #if defined(__clang__) && defined(__has_warning) #if __has_warning("-Wself-assign-overloaded") -DOCTEST_CLANG_SUPPRESS_WARNING_PUSH -DOCTEST_CLANG_SUPPRESS_WARNING("-Wself-assign-overloaded") + DOCTEST_CLANG_SUPPRESS_WARNING_PUSH + DOCTEST_CLANG_SUPPRESS_WARNING("-Wself-assign-overloaded") #endif #endif @@ -1300,7 +1300,7 @@ TEST_CASE("regression test #5122 - nlohmann::ordered_map copy-assignment is self #if defined(__clang__) && defined(__has_warning) #if __has_warning("-Wself-assign-overloaded") -DOCTEST_CLANG_SUPPRESS_WARNING_POP + DOCTEST_CLANG_SUPPRESS_WARNING_POP #endif #endif @@ -1323,7 +1323,7 @@ TEST_CASE("regression test #5122 - nlohmann::ordered_map move-assignment transfe CHECK(it->second == "2"); // Re-assigning into the moved-from object must leave it in a usable state. - src = nlohmann::ordered_map{}; + src = nlohmann::ordered_map {}; src.emplace("after-move", "3"); REQUIRE(src.size() == 1); CHECK(src.begin()->first == "after-move");