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

ordered json pointer corruption #4289

Open
2 tasks
mishase opened this issue Feb 7, 2024 · 5 comments
Open
2 tasks

ordered json pointer corruption #4289

mishase opened this issue Feb 7, 2024 · 5 comments
Labels
documentation solution: duplicate the issue is a duplicate; refer to the linked issue instead

Comments

@mishase
Copy link

mishase commented Feb 7, 2024

Description

When using ordered json with push_back call, pointer to previous structure gets corrupted

Reproduction steps

Use the code bellow with nlohmann::ordered_json to get corrupted pointer

Expected vs. actual results

Expected output (same code, without ordered json):

string
string
{"a":{"b":"test"},"c":[1]}

Actual output (corrupted pointer):

string
number
{"a":{"b":"test"},"c":[1]}

Minimal code example

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::ordered_json; // use nlohmann::json to get correct result

int main()
{
	auto obj = json::parse("{\"a\": {\"b\": \"test\"}}");
		
	auto& ref = obj["a"]["b"];
	std::cout << ref.type_name() << '\n'; // string

	obj["c"] = json::array();
	obj["c"].push_back(1);

	std::cout << ref.type_name() << '\n'; // expected string, got number

	std::cout << obj.dump(); // valid result, {"a":{"b":"test"},"c":[1]}
}

Error messages

No response

Compiler and operating system

Tested on ARM Windows and X64 Windows in Debug and Release modes

Library version

3.11.3

Validation

@nlohmann
Copy link
Owner

nlohmann commented Feb 7, 2024

This seems related to #4279.

@nlohmann nlohmann added documentation solution: duplicate the issue is a duplicate; refer to the linked issue instead and removed kind: bug labels Feb 7, 2024
@gregmarr
Copy link
Contributor

gregmarr commented Feb 7, 2024

As in the linked issue, you can't do that. The push_back function on vector can invalidate pointers, and ordered_json uses a vector.

@mishase
Copy link
Author

mishase commented Feb 8, 2024

It is related to the issue you mentioned earlier, but it's certainly NOT a documentation issue. ordered_json must have working push_back function without any pointer corruption or this function must be private if it can cause such behaviour. Please fix this

@nlohmann
Copy link
Owner

nlohmann commented Feb 8, 2024

I have no idea how to fix it, so documentation is currently all we can do.

@gregmarr
Copy link
Contributor

gregmarr commented Feb 9, 2024

@mishase This behavior comes from std::vector<>::push_back(), so not only is it not broken, it is standards conforming.

https://en.cppreference.com/w/cpp/container/vector/push_back

If after the operation the new size() is greater than old capacity() a reallocation takes place, in which case all iterators (including the end() iterator) and all references to the elements are invalidated. Otherwise only the end() iterator is invalidated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation solution: duplicate the issue is a duplicate; refer to the linked issue instead
Projects
None yet
Development

No branches or pull requests

3 participants