-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Assertion failed when accessing non-existing object with const json object #4297
Comments
The issue is the const. See https://json.nlohmann.me/api/basic_json/operator%5B%5D/#notes |
This may be a stupid question but I'll ask it anyway: why not raise an exception instead of using assert() ? |
See https://json.nlohmann.me/features/element_access/unchecked_access/ design rationale |
It seems to me to be a design choice to have this undefined behavior. |
As the caller, you have the ability to detect whether or not the item is there before calling There is no |
I have indeed the ability to check for each element presence, but being able to process a whole message with one The non-const version of Concerning the |
If you don't have error management for each one, then attempting to read a non-existing element aborts the whole operation. If that's what you want, then this would give you the same behavior you are seeking from
If that's NOT what you want, and you just want a default value when the child isn't there, you should just use the
Removing the non-const version would match
Others have different priorities. With the current behavior, you have the ability to add that overhead if you want/need it. Adding the check means that others that don't want the overhead can't avoid it.
That is absolutely not true, you can iterate over the dictionary just as well. |
I think this whole discussion is about priorities. To me having an undefined behavior by just adding a You will probably tell me that there is no issue since this is documented, but for a library which is intended to be largely used I believe it is an issue. |
Well, the design decision is documented, and this is the behavior of the library since day 1. I can understand your point, but using unchecked access for a non-existing key is a problem in itself. The library offers (and documents) several APIs how to cope with this. And what do you expect when you access
|
If all cases are already covered with I'm insisting maybe too much on this, but I'm used to MISRA coding rules which require variables to be declared as |
No: operator[] is for unchecked access. Don't pay for what you don't use. |
Fine, but I still deeply believe this is a bad design choice. |
Description
When I do a copy of a subpart of a json and then access an element which does not exists an assertion failed is trigger instead of an exception.
Trying to access the non-existing element before seems to prevent the issue (see reproduction steps for explanations).
Reproduction steps
With the following code:
My program gets interrupted with the following message;
Now if I add an extra step accessing the non-existing item:
I got an exception instead of an assertion failed:
Expected vs. actual results
The assertion failed shall not be raised.
Minimal code example
See "Reproduction steps"
Error messages
See "Reproduction steps"
Compiler and operating system
g++ 7.5.0 on Ubuntu 18.04
Library version
3.11.3
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: