From b9dfdbe6be73bfa57e024ac45f7a184354f0f6f5 Mon Sep 17 00:00:00 2001 From: Xav83 Date: Thu, 19 Sep 2019 17:46:10 +0200 Subject: [PATCH 1/3] Correct a warning from cppcheck: binary_reader.hpp:650: (style) Unsigned expression 'mant' can't be negative so it is unnecessary to test it https://github.com/Xav83/nlohmann-json-cppcheck/commit/910a7d2b873dd7ae92ec81cced2bf73200ff4848/checks#step:5:84 Signed-off-by: Xav83 --- include/nlohmann/detail/input/binary_reader.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 22dc55d844..f33d89cb69 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -647,7 +647,7 @@ class binary_reader const int exp = (half >> 10u) & 0x1Fu; const unsigned int mant = half & 0x3FFu; assert(0 <= exp and exp <= 32); - assert(0 <= mant and mant <= 1024); + assert(mant <= 1024); switch (exp) { case 0: From 13a7c602578878bd524da551a1005b92e8d8e79b Mon Sep 17 00:00:00 2001 From: Xav83 Date: Thu, 19 Sep 2019 17:58:41 +0200 Subject: [PATCH 2/3] Correct a warning from cppcheck: binary_writer.hpp:869: (style) Consider using std::accumulate algorithm instead of a raw loop. https://github.com/Xav83/nlohmann-json-cppcheck/commit/910a7d2b873dd7ae92ec81cced2bf73200ff4848/checks#step:5:107 Signed-off-by: Xav83 --- include/nlohmann/detail/output/binary_writer.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 29093dd372..eb312e8a47 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -861,13 +861,12 @@ class binary_writer */ static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value) { - std::size_t embedded_document_size = 0ul; std::size_t array_index = 0ul; - for (const auto& el : value) + const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), 0ul, [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el) { - embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el); - } + return result + calc_bson_element_size(std::to_string(array_index++), el); + }); return sizeof(std::int32_t) + embedded_document_size + 1ul; } From 87afee1c39ec62ac49dde2f3c8714bdfbeac0896 Mon Sep 17 00:00:00 2001 From: Xav83 Date: Thu, 19 Sep 2019 19:25:39 +0200 Subject: [PATCH 3/3] Runs make amalgamate on the project. --- single_include/nlohmann/json.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bdd3642f17..d27940b85c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5678,7 +5678,7 @@ class binary_reader const int exp = (half >> 10u) & 0x1Fu; const unsigned int mant = half & 0x3FFu; assert(0 <= exp and exp <= 32); - assert(0 <= mant and mant <= 1024); + assert(mant <= 1024); switch (exp) { case 0: @@ -12044,13 +12044,12 @@ class binary_writer */ static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value) { - std::size_t embedded_document_size = 0ul; std::size_t array_index = 0ul; - for (const auto& el : value) + const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), 0ul, [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el) { - embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el); - } + return result + calc_bson_element_size(std::to_string(array_index++), el); + }); return sizeof(std::int32_t) + embedded_document_size + 1ul; }