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

Yet another assertion failure when inserting into arrays with JSON_DIAGNOSTICS set #3032

Closed
1 of 5 tasks
carlsmedstad opened this issue Sep 25, 2021 · 1 comment · Fixed by #3037
Closed
1 of 5 tasks
Assignees
Labels
kind: bug release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@carlsmedstad
Copy link
Contributor

Inserting into arrays using j_array[index] = value; syntax does not correctly update the parents and will trigger the assertion for parent correctness further down the line.

Please describe the steps to reproduce the issue.

Minimal example:

#define JSON_DIAGNOSTICS 1

#include <nlohmann/json.hpp>

using json = nlohmann::json;


int main() {
  json j_array = json::array();
  j_array[0] = "STRING";
  j_array[1] = "STRING";
  json j_array_copy = j_array;
  return 0;
}

This produces the following assertion error when the copying from j_array to j_array_copy is performed:

assertion_failure: single_include/nlohmann/json.hpp:18670: void nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::assert_invariant(bool) const [with ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>]: Assertion `!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) { return j.m_parent == this; })' failed.

Which compiler and operating system are you using?

  • Compiler: g++ (GCC) 11.1.0
  • Operating system: Linux

Which version of the library did you use?

  • latest release version 3.10.2
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
@gregmarr
Copy link
Contributor

Looks like we need a capacity change check at

m_value.array->resize(idx + 1);

carlsmedstad added a commit to carlsmedstad/json that referenced this issue Sep 27, 2021
Additionally, add the previosly failing case to the test suite.
carlsmedstad added a commit to carlsmedstad/json that referenced this issue Sep 27, 2021
Additionally, add the previously failing case to the test suite.
carlsmedstad added a commit to carlsmedstad/json that referenced this issue Sep 27, 2021
Additionally, add the previously failing case to the test suite.
carlsmedstad added a commit to carlsmedstad/json that referenced this issue Sep 28, 2021
Additionally, add the previously failing case to the test suite.
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 1, 2021
@nlohmann nlohmann self-assigned this Oct 7, 2021
@nlohmann nlohmann added this to the Release 3.10.3 milestone Oct 7, 2021
nlohmann pushed a commit that referenced this issue Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants