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

Referencing a subkey which doesn't exist gives crash #2797

Closed
sreerajchundayil opened this issue Jun 1, 2021 · 2 comments
Closed

Referencing a subkey which doesn't exist gives crash #2797

sreerajchundayil opened this issue Jun 1, 2021 · 2 comments

Comments

@sreerajchundayil
Copy link

sreerajchundayil commented Jun 1, 2021

I am not sure whether this is a real bug or undefined behaviour, it's related to referencing a subkey which doesn't exist including its parent.

My understanding is that when I try to do a C++ reference of a json object , if the key doesn't exist it will insert the key
and give back the reference of it.
In my example I have a main object json js = null;
Now I am trying to do json& newJs = js["key1"]["key2"];
Later when I try to insert some object to this newJs, it crashes.
I overcame this issue by doing first js["key1"]["key2"] = null and then giving reference of it.

If you think this is not an undefined behaviour, I will try to replicate the issue in simpler code.

@t-b
Copy link
Contributor

t-b commented Jun 1, 2021

This is documented behaviour, see https://nlohmann.github.io/json/api/basic_json/operator%5B%5D/. Use https://nlohmann.github.io/json/api/basic_json/at/ which does handle non-existing keys gracefully.

@sreerajchundayil
Copy link
Author

It's explained in documentation. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants