From 1fb725a48863f40d9f4a6b4dde2fcb351d6013ed 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 3b2180574..50036d000 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 93be3e8ff..89b0a77a1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -21427,6 +21427,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // all_of +#include // array #include // isdigit #include // errno, ERANGE #include // size_t @@ -22695,15 +22696,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); } /*!