From def1acf5b3554f3468bbc66198dd9563fbbebe6d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 29 Nov 2026 10:48:04 +0100 Subject: [PATCH] Fix cpplint and old libstdc++ in the one-block input read Use std::uint64_t and auto instead of long types (cpplint runtime/int), and reserve only when the request is larger than the capacity: before GCC 11, libstdc++ shrinks a string on a smaller reserve and moves the owned buffer. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/input.hpp | 28 ++++++++++++++++++-------- single_include/nlohmann/json_view.hpp | 28 ++++++++++++++++++-------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/include/nlohmann/detail/view/input.hpp b/include/nlohmann/detail/view/input.hpp index b222e41af..319e5168b 100644 --- a/include/nlohmann/detail/view/input.hpp +++ b/include/nlohmann/detail/view/input.hpp @@ -10,6 +10,7 @@ #include // max #include // size_t +#include // uint64_t #ifndef JSON_NO_IO #include // FILE, fread, ftell #if defined(_WIN32) @@ -148,6 +149,17 @@ constexpr std::size_t read_block_size() noexcept return 4096; } +/// make room for at least n characters in buf: never asks for less than its +/// capacity, because before C++20 such a request may shrink the string (as +/// libstdc++ before GCC 11 does) and move its characters +inline void reserve_at_least(std::string& buf, std::size_t n) +{ + if (n > buf.capacity()) + { + buf.reserve(n); + } +} + /// append the rest of a stream: its characters need no conversion, so they are /// read in blocks from the stream buffer, straight into buf (reusing its /// capacity). As the library's stream adapter, this reads the stream buffer @@ -167,7 +179,7 @@ inline void append_stream(std::string& buf, std::istream& is) const std::streamsize rest = sb->in_avail(); if (rest > 0 && static_cast(rest) < buf.max_size() - buf.size() - read_block_size()) { - buf.reserve(buf.size() + static_cast(rest) + read_block_size()); + reserve_at_least(buf, buf.size() + static_cast(rest) + read_block_size()); } for (;;) { @@ -198,26 +210,26 @@ struct is_c_stream /// the number of bytes left in a C stream that is a regular file (from its /// size and position), 0 for anything else and on other systems: a hint for /// the room to reserve; f is not null -inline unsigned long long file_rest(std::FILE* f) noexcept +inline std::uint64_t file_rest(std::FILE* f) noexcept { #if defined(_WIN32) struct _stat64 st {}; if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG) { - const long long here = ::_ftelli64(f); + const auto here = ::_ftelli64(f); if (here >= 0 && st.st_size > here) { - return static_cast(st.st_size - here); + return static_cast(st.st_size - here); } } #elif defined(__unix__) || defined(__APPLE__) struct stat st {}; if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode)) { - const long here = std::ftell(f); + const auto here = std::ftell(f); if (here >= 0 && st.st_size > here) { - return static_cast(st.st_size - here); + return static_cast(st.st_size - here); } } #else @@ -235,10 +247,10 @@ inline void append_file(std::string& buf, std::FILE* f) // the file is then read in one block, without a reallocation and copy at // each doubling of the string (only a hint: the loop below reads what is // there) - const unsigned long long rest = file_rest(f); + const std::uint64_t rest = file_rest(f); if (rest > 0 && rest < buf.max_size() - buf.size() - 1) { - buf.reserve(buf.size() + static_cast(rest) + 1); + reserve_at_least(buf, buf.size() + static_cast(rest) + 1); } for (;;) { diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 3f46d05d5..fa0312bc8 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -7956,6 +7956,7 @@ NLOHMANN_JSON_NAMESPACE_END #include // max #include // size_t +#include // uint64_t #ifndef JSON_NO_IO #include // FILE, fread, ftell #if defined(_WIN32) @@ -8095,6 +8096,17 @@ constexpr std::size_t read_block_size() noexcept return 4096; } +/// make room for at least n characters in buf: never asks for less than its +/// capacity, because before C++20 such a request may shrink the string (as +/// libstdc++ before GCC 11 does) and move its characters +inline void reserve_at_least(std::string& buf, std::size_t n) +{ + if (n > buf.capacity()) + { + buf.reserve(n); + } +} + /// append the rest of a stream: its characters need no conversion, so they are /// read in blocks from the stream buffer, straight into buf (reusing its /// capacity). As the library's stream adapter, this reads the stream buffer @@ -8114,7 +8126,7 @@ inline void append_stream(std::string& buf, std::istream& is) const std::streamsize rest = sb->in_avail(); if (rest > 0 && static_cast(rest) < buf.max_size() - buf.size() - read_block_size()) { - buf.reserve(buf.size() + static_cast(rest) + read_block_size()); + reserve_at_least(buf, buf.size() + static_cast(rest) + read_block_size()); } for (;;) { @@ -8145,26 +8157,26 @@ struct is_c_stream /// the number of bytes left in a C stream that is a regular file (from its /// size and position), 0 for anything else and on other systems: a hint for /// the room to reserve; f is not null -inline unsigned long long file_rest(std::FILE* f) noexcept +inline std::uint64_t file_rest(std::FILE* f) noexcept { #if defined(_WIN32) struct _stat64 st {}; if (::_fstat64(_fileno(f), &st) == 0 && (st.st_mode & _S_IFMT) == _S_IFREG) { - const long long here = ::_ftelli64(f); + const auto here = ::_ftelli64(f); if (here >= 0 && st.st_size > here) { - return static_cast(st.st_size - here); + return static_cast(st.st_size - here); } } #elif defined(__unix__) || defined(__APPLE__) struct stat st {}; if (::fstat(fileno(f), &st) == 0 && S_ISREG(st.st_mode)) { - const long here = std::ftell(f); + const auto here = std::ftell(f); if (here >= 0 && st.st_size > here) { - return static_cast(st.st_size - here); + return static_cast(st.st_size - here); } } #else @@ -8182,10 +8194,10 @@ inline void append_file(std::string& buf, std::FILE* f) // the file is then read in one block, without a reallocation and copy at // each doubling of the string (only a hint: the loop below reads what is // there) - const unsigned long long rest = file_rest(f); + const std::uint64_t rest = file_rest(f); if (rest > 0 && rest < buf.max_size() - buf.size() - 1) { - buf.reserve(buf.size() + static_cast(rest) + 1); + reserve_at_least(buf, buf.size() + static_cast(rest) + 1); } for (;;) {