Skip to content

Commit

Permalink
Fix assertion failure nlohmann#3032
Browse files Browse the repository at this point in the history
Additionally, add the previously failing case to the test suite.
  • Loading branch information
carlsmedstad committed Sep 27, 2021
1 parent 0b345b2 commit 1195701
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
15 changes: 12 additions & 3 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3695,14 +3695,23 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
if (idx >= m_value.array->size())
{
#if JSON_DIAGNOSTICS
// remember array size before resizing
// remember array size & capacity before resizing
const auto previous_size = m_value.array->size();
const auto old_capacity = m_value.array->capacity();
#endif
m_value.array->resize(idx + 1);

#if JSON_DIAGNOSTICS
// set parent for values added above
set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
{
// capacity has changed: update all parents
set_parents();
}
else
{
// set parent for values added above
set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
}
#endif
}

Expand Down
15 changes: 12 additions & 3 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21101,14 +21101,23 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
if (idx >= m_value.array->size())
{
#if JSON_DIAGNOSTICS
// remember array size before resizing
// remember array size & capacity before resizing
const auto previous_size = m_value.array->size();
const auto old_capacity = m_value.array->capacity();
#endif
m_value.array->resize(idx + 1);

#if JSON_DIAGNOSTICS
// set parent for values added above
set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity))
{
// capacity has changed: update all parents
set_parents();
}
else
{
// set parent for values added above
set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
}
#endif
}

Expand Down
13 changes: 13 additions & 0 deletions test/src/unit-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,17 @@ TEST_CASE("Better diagnostics")
root.push_back(lower);
}
}

SECTION("Regression test for https://github.com/nlohmann/json/issues/3032")
{
// reference operator[](size_type idx)
{
json j_arr = json::array();
j_arr[0] = "STRING";
j_arr[1] = "STRING";
j_arr[2] = "STRING";
j_arr[4] = "STRING";
json j_arr_copy = j_arr;
}
}
}

0 comments on commit 1195701

Please sign in to comment.