From badbe685a5340579035dd8b30292980f5e2c6717 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 17 Oct 2026 23:07:04 +0200 Subject: [PATCH] Do not copy an empty run into an unallocated arena memcpy() must not be passed a null pointer even for zero bytes, which arena_run() did for an empty run while the arena was still empty (ci_test_clang_sanitizer: "null pointer passed as argument 1" in builder.hpp). Only the slow path can see such a run. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/builder.hpp | 4 +++- single_include/nlohmann/json_view.hpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/view/builder.hpp b/include/nlohmann/detail/view/builder.hpp index 11187a947..eb3d70792 100644 --- a/include/nlohmann/detail/view/builder.hpp +++ b/include/nlohmann/detail/view/builder.hpp @@ -415,8 +415,10 @@ class builder arena_ensure(16); std::memcpy(m_arena_write, r, 16); } - else + else if (n != 0) { + // (an empty run leaves an empty arena unallocated, and memcpy() + // must not be passed its null pointer even for zero bytes) arena_ensure(n); std::memcpy(m_arena_write, r, n); } diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 26e30cbbb..0de60773b 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -1039,8 +1039,10 @@ class builder arena_ensure(16); std::memcpy(m_arena_write, r, 16); } - else + else if (n != 0) { + // (an empty run leaves an empty arena unallocated, and memcpy() + // must not be passed its null pointer even for zero bytes) arena_ensure(n); std::memcpy(m_arena_write, r, n); }