Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assertion failure for JSON_DIAGNOSTICS #3037

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3695,15 +3695,25 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
if (idx >= m_value.array->size())
{
#if JSON_DIAGNOSTICS
// remember array size before resizing
const auto previous_size = m_value.array->size();
// remember array size & capacity before resizing
const auto old_size = m_value.array->size();
const auto old_capacity = m_value.array->capacity();
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
#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>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
}
carlsmedstad marked this conversation as resolved.
Show resolved Hide resolved
#endif
assert_invariant();
}

return m_value.array->operator[](idx);
Expand Down
18 changes: 14 additions & 4 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21101,15 +21101,25 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
if (idx >= m_value.array->size())
{
#if JSON_DIAGNOSTICS
// remember array size before resizing
const auto previous_size = m_value.array->size();
// remember array size & capacity before resizing
const auto old_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>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
}
#endif
assert_invariant();
}

return m_value.array->operator[](idx);
Expand Down
17 changes: 17 additions & 0 deletions test/src/unit-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,21 @@ 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] = 0;
j_arr[1] = 1;
j_arr[2] = 2;
j_arr[3] = 3;
j_arr[4] = 4;
j_arr[5] = 5;
j_arr[6] = 6;
j_arr[7] = 7;
json j_arr_copy = j_arr;
}
}
}