From e1aa3932264f49d0a1a90d0574b8a3f01e1dda75 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 27 Oct 2026 08:05:36 +0100 Subject: [PATCH] Address review comments on operator[](json_pointer) rollback - undo() shrinks the array with resize() instead of erase() - comments say what "will be" done in get_unchecked_create - tests: avoid a clang-tidy performance-unnecessary-copy-initialization hit (unit-alt-string) and the word "access" in section names, which Flawfinder flags as race/access Signed-off-by: Niels Lohmann --- include/nlohmann/detail/json_pointer.hpp | 8 ++++---- single_include/nlohmann/json.hpp | 8 ++++---- tests/src/unit-alt-string.cpp | 6 +++--- tests/src/unit-json_pointer.cpp | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 2747a6e5b..72966ef6e 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -810,7 +810,7 @@ class json_pointer case change::array_grown: { auto& elements = *m_value->m_data.m_value.array; - elements.erase(std::next(elements.begin(), static_cast(m_old_size)), elements.end()); + elements.resize(m_old_size); break; } case change::none: @@ -1271,7 +1271,7 @@ class json_pointer // explicitly treat "-" as index beyond the end return array_append(*ptr); } - // an element is appended in get_unchecked_create + // an element will be appended in get_unchecked_create break; } if (token + 1 == end) @@ -1282,14 +1282,14 @@ class json_pointer const auto idx = array_index(*token); if (idx >= ptr->m_data.m_value.array->size()) { - // the array is padded in get_unchecked_create + // the array will be padded in get_unchecked_create break; } ptr = &(*ptr->m_data.m_value.array)[idx]; } else if (ptr->is_null()) { - // the null value is converted in get_unchecked_create + // the null value will be converted in get_unchecked_create break; } else diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index a3546bf6b..cd9d5a42a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -24875,7 +24875,7 @@ class json_pointer case change::array_grown: { auto& elements = *m_value->m_data.m_value.array; - elements.erase(std::next(elements.begin(), static_cast(m_old_size)), elements.end()); + elements.resize(m_old_size); break; } case change::none: @@ -25336,7 +25336,7 @@ class json_pointer // explicitly treat "-" as index beyond the end return array_append(*ptr); } - // an element is appended in get_unchecked_create + // an element will be appended in get_unchecked_create break; } if (token + 1 == end) @@ -25347,14 +25347,14 @@ class json_pointer const auto idx = array_index(*token); if (idx >= ptr->m_data.m_value.array->size()) { - // the array is padded in get_unchecked_create + // the array will be padded in get_unchecked_create break; } ptr = &(*ptr->m_data.m_value.array)[idx]; } else if (ptr->is_null()) { - // the null value is converted in get_unchecked_create + // the null value will be converted in get_unchecked_create break; } else diff --git a/tests/src/unit-alt-string.cpp b/tests/src/unit-alt-string.cpp index 4db427f9b..75feb74e8 100644 --- a/tests/src/unit-alt-string.cpp +++ b/tests/src/unit-alt-string.cpp @@ -385,9 +385,9 @@ TEST_CASE("alternative string type") CHECK(j.dump() == R"({"a":{"b":[1,null,null,{"x":[{"y":1}]},4],"z":2},"c":[{"q":3}]})"); // a failing access leaves the value unchanged - alt_json k = before; - CHECK_THROWS_AS(k[alt_json::json_pointer("/a/n/99999999999999999999999")], alt_json::out_of_range&); - CHECK(k == before); + j = before; + CHECK_THROWS_AS(j[alt_json::json_pointer("/a/n/99999999999999999999999")], alt_json::out_of_range&); + CHECK(j == before); } SECTION("operator/(std::size_t)") diff --git a/tests/src/unit-json_pointer.cpp b/tests/src/unit-json_pointer.cpp index 4190c5883..81c588544 100644 --- a/tests/src/unit-json_pointer.cpp +++ b/tests/src/unit-json_pointer.cpp @@ -613,13 +613,13 @@ TEST_CASE("JSON pointers") "[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", json::parse_error&); } - SECTION("a failing access leaves the value unchanged") + SECTION("a failing operator[] leaves the value unchanged") { check_strong_guarantee_cases(); check_strong_guarantee_cases(); } - SECTION("a successful access still creates the same values") + SECTION("a successful operator[] still creates the same values") { json j = {{"a", {1}}}; j["/a/3/x/-/y"_json_pointer] = 1;