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

JSON_DIAGNOSTICS trigger assertion #3915

Closed
2 tasks
xamix opened this issue Jan 10, 2023 · 3 comments · Fixed by #4537
Closed
2 tasks

JSON_DIAGNOSTICS trigger assertion #3915

xamix opened this issue Jan 10, 2023 · 3 comments · Fixed by #4537
Assignees
Labels
confirmed kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@xamix
Copy link

xamix commented Jan 10, 2023

Description

Hi,

I have activated the JSON_DIAGNOSTICS=1 globally in my project and ASSERTION are now fired at runtime.
I tested it on with 3.10.5 and then with latest release 3.11.2.

I have done a minimal example to reproduce the assertion (see minimal code example below)

Is there something wrong with my code?

Reproduction steps

Run the minimal code example below

Expected vs. actual results

No ASSERTION should be triggered since without the JSON_DIAGNOSTICS enabled, all seem to work without any memory issues

Minimal code example

json j = json::object();
j["root"] = "root_str";

json jj = json::object();
jj["child"] = json::object();

// If do not push anything in object, then no assert will be produced
jj["child"]["prop1"] = "prop1_value";

// Push all properties of child in parent
j.insert(jj.at("child").begin(), jj.at("child").end());

// Here assert is generated when construct new json 
json k(j);

Error messages

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

Compiler and operating system

Clang-10 on Ubuntu 20.04

Library version

3.11.2

Validation

@nlohmann
Copy link
Owner

I can confirm the issue.

@Romop5
Copy link

Romop5 commented Jun 3, 2023

If I understood the code correctly, as of json k(j); the invariant is invalid for instance j due to function insert(it, it) missing a call to set_parents() after this line of code: https://github.com/nlohmann/json/blob/develop/include/nlohmann/json.hpp#L3408

Provided I got the meaning of the statement, during insertion, the properties are basically copied (inserted) to the underlying data structure ObjectType (being std::map for default nlohmann::json) and the copies keep the original parent (as no set_parents() is called during insertion).

@nlohmann nlohmann self-assigned this Dec 12, 2024
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 12, 2024
@nlohmann
Copy link
Owner

@Romop5 Thanks for the analysis! I created a fix. Please take a look at #4537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed kind: bug 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