From f941cf7a7ebf3422e42531dab32b02578bad61eb Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 16 Oct 2026 17:19:33 +0200 Subject: [PATCH] Use std::array for the index buffer in append_index() Signed-off-by: Niels Lohmann --- include/nlohmann/detail/json_pointer.hpp | 7 ++++--- single_include/nlohmann/json.hpp | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 3c832268e..f3c262cba 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -9,6 +9,7 @@ #pragma once #include // all_of +#include // array #include // isdigit #include // errno, ERANGE #include // size_t @@ -1270,15 +1271,15 @@ class json_pointer /// append the decimal representation of @a i to @a out static void append_index(string_t& out, std::size_t i) { - char buf[24]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - std::size_t pos = sizeof(buf); + std::array buf{}; + std::size_t pos = buf.size(); do { buf[--pos] = static_cast('0' + i % 10); i /= 10; } while (i != 0); - out.append(buf + pos, sizeof(buf) - pos); + out.append(buf.data() + pos, buf.size() - pos); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c55c08897..dbe7a6048 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20974,6 +20974,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // all_of +#include // array #include // isdigit #include // errno, ERANGE #include // size_t @@ -22242,15 +22243,15 @@ class json_pointer /// append the decimal representation of @a i to @a out static void append_index(string_t& out, std::size_t i) { - char buf[24]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - std::size_t pos = sizeof(buf); + std::array buf{}; + std::size_t pos = buf.size(); do { buf[--pos] = static_cast('0' + i % 10); i /= 10; } while (i != 0); - out.append(buf + pos, sizeof(buf) - pos); + out.append(buf.data() + pos, buf.size() - pos); } /*!