mirror of
https://github.com/nlohmann/json.git
synced 2026-10-27 16:28:57 +00:00
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 <mail@nlohmann.me>
This commit is contained in:
4 files changed
+13
-13
No files matched your search
@@ -810,7 +810,7 @@ class json_pointer
|
|||||||
case change::array_grown:
|
case change::array_grown:
|
||||||
{
|
{
|
||||||
auto& elements = *m_value->m_data.m_value.array;
|
auto& elements = *m_value->m_data.m_value.array;
|
||||||
elements.erase(std::next(elements.begin(), static_cast<typename BasicJsonType::difference_type>(m_old_size)), elements.end());
|
elements.resize(m_old_size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case change::none:
|
case change::none:
|
||||||
@@ -1271,7 +1271,7 @@ class json_pointer
|
|||||||
// explicitly treat "-" as index beyond the end
|
// explicitly treat "-" as index beyond the end
|
||||||
return array_append(*ptr);
|
return array_append(*ptr);
|
||||||
}
|
}
|
||||||
// an element is appended in get_unchecked_create
|
// an element will be appended in get_unchecked_create
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (token + 1 == end)
|
if (token + 1 == end)
|
||||||
@@ -1282,14 +1282,14 @@ class json_pointer
|
|||||||
const auto idx = array_index<BasicJsonType>(*token);
|
const auto idx = array_index<BasicJsonType>(*token);
|
||||||
if (idx >= ptr->m_data.m_value.array->size())
|
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;
|
break;
|
||||||
}
|
}
|
||||||
ptr = &(*ptr->m_data.m_value.array)[idx];
|
ptr = &(*ptr->m_data.m_value.array)[idx];
|
||||||
}
|
}
|
||||||
else if (ptr->is_null())
|
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;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Generated
+4
-4
@@ -24875,7 +24875,7 @@ class json_pointer
|
|||||||
case change::array_grown:
|
case change::array_grown:
|
||||||
{
|
{
|
||||||
auto& elements = *m_value->m_data.m_value.array;
|
auto& elements = *m_value->m_data.m_value.array;
|
||||||
elements.erase(std::next(elements.begin(), static_cast<typename BasicJsonType::difference_type>(m_old_size)), elements.end());
|
elements.resize(m_old_size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case change::none:
|
case change::none:
|
||||||
@@ -25336,7 +25336,7 @@ class json_pointer
|
|||||||
// explicitly treat "-" as index beyond the end
|
// explicitly treat "-" as index beyond the end
|
||||||
return array_append(*ptr);
|
return array_append(*ptr);
|
||||||
}
|
}
|
||||||
// an element is appended in get_unchecked_create
|
// an element will be appended in get_unchecked_create
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (token + 1 == end)
|
if (token + 1 == end)
|
||||||
@@ -25347,14 +25347,14 @@ class json_pointer
|
|||||||
const auto idx = array_index<BasicJsonType>(*token);
|
const auto idx = array_index<BasicJsonType>(*token);
|
||||||
if (idx >= ptr->m_data.m_value.array->size())
|
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;
|
break;
|
||||||
}
|
}
|
||||||
ptr = &(*ptr->m_data.m_value.array)[idx];
|
ptr = &(*ptr->m_data.m_value.array)[idx];
|
||||||
}
|
}
|
||||||
else if (ptr->is_null())
|
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;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -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}]})");
|
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
|
// a failing access leaves the value unchanged
|
||||||
alt_json k = before;
|
j = before;
|
||||||
CHECK_THROWS_AS(k[alt_json::json_pointer("/a/n/99999999999999999999999")], alt_json::out_of_range&);
|
CHECK_THROWS_AS(j[alt_json::json_pointer("/a/n/99999999999999999999999")], alt_json::out_of_range&);
|
||||||
CHECK(k == before);
|
CHECK(j == before);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("operator/(std::size_t)")
|
SECTION("operator/(std::size_t)")
|
||||||
|
|||||||
@@ -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&);
|
"[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<json>();
|
check_strong_guarantee_cases<json>();
|
||||||
check_strong_guarantee_cases<nlohmann::ordered_json>();
|
check_strong_guarantee_cases<nlohmann::ordered_json>();
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("a successful access still creates the same values")
|
SECTION("a successful operator[] still creates the same values")
|
||||||
{
|
{
|
||||||
json j = {{"a", {1}}};
|
json j = {{"a", {1}}};
|
||||||
j["/a/3/x/-/y"_json_pointer] = 1;
|
j["/a/3/x/-/y"_json_pointer] = 1;
|
||||||
|
|||||||
Reference in new issue
Block a user