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

Dumping core when reading invalid file #1975

Closed
ilya1725 opened this issue Mar 6, 2020 · 4 comments
Closed

Dumping core when reading invalid file #1975

ilya1725 opened this issue Mar 6, 2020 · 4 comments

Comments

@ilya1725
Copy link

ilya1725 commented Mar 6, 2020

Hello:

I have your module in my application. Compiled with exceptions disabled and -O2 optimization.
I was trying to read an invalid JSON file:

{ "sn": 1, "name": "First bird" "type": 1, "flags": { "tied_down": true } }

const std::ifstream config_file(argv[1]); nlohmann::json j; config_file >> j;

The application dumps core in the last line instead of returning an error:

Program received signal SIGABRT, Aborted. __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 50 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory. (gdb) bt #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 #1 0x00007f0de58f8899 in __GI_abort () at abort.c:79 #2 0x000055d061bb677d in nlohmann::detail::json_sax_dom_callback_parser<nlohmann::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, nlohmann::adl_serializer> >::parse_error (this=<optimized out>, this=<optimized out>, ex=...) at /usr/include/c++/9/bits/basic_string.h:1936 #3 nlohmann::detail::json_sax_dom_parser<nlohmann::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, nlohmann::adl_serializer> >::parse_error (this=<optimized out>, this=<optimized out>, ex=...) at /develop/build/_deps/nlohmann_json-src/single_include/nlohmann/json.hpp:4806 #4 nlohmann::detail::parser<nlohmann::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, nlohmann::adl_serializer> >::sax_parse_internal<nlohmann::detail::json_sax_dom_parser<nlohmann::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, nlohmann::adl_serializer> > > (this=<optimized out>, sax=<optimized out>) at /develop/build/_deps/nlohmann_json-src/single_include/nlohmann/json.hpp:8987 #5 0x000055d061bb6dbc in nlohmann::detail::parser<nlohmann::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, nlohmann::adl_serializer> >::parse ( this=0x7fff73960a80, strict=<optimized out>, result=...) at /usr/include/c++/9/bits/stl_vector.h:94 #6 0x000055d061bb76c3 in nlohmann::operator>> (i=..., j=...) at /develop/build/_deps/nlohmann_json-src/single_include/nlohmann/json.hpp:9104 #7 0x000055d061ba888a in main (argc=2, argv=0x7fff73960ee8) at /develop/apps/apps/test_application/src/test_application.cpp:33

  1. Is this expected behavior?
  2. If yes, what is the proper usage of the class so that the application doesn't dump core and returns an error instead?

Thank you.

@ilya1725 ilya1725 changed the title Dumping code when reading invalid file Dumping core when reading invalid file Mar 6, 2020
@t-b
Copy link
Contributor

t-b commented Mar 7, 2020

The following example https://godbolt.org/z/zpxg_c does not dump core. Any idea what I'm doing wrong?

@dota17
Copy link
Contributor

dota17 commented Mar 9, 2020

Hi @ilya1725 , this core dumpedbehavior is expected, the reason is very simple: The input file should be attached when executing the code., like./a.out input_data.json

@ilya1725 ilya1725 closed this as completed Mar 9, 2020
@ilya1725
Copy link
Author

ilya1725 commented Mar 9, 2020

@dota17 , sorry, what? The JSON source file is attached fine. This isn't the problem.

@t-b: I've tried the code in the example and it dumps core just fine. The Compiler Explorer just doesn't show it. One way to avoid the direct core dump is to set the allow_exceptions = true. In this case, if there is an error, the return value will have the discarded flag set. It is not clear from the documentation how to handle this case. An example would have been nice. And this method only works with direct parse(). It won't work with the stream parsing.

@dota17
Copy link
Contributor

dota17 commented Mar 10, 2020

@ilya1725 Hmmm, you're right, I got a hasty conclusion, my last point is really wrong, even though they are the same types of errors.
Using operator>> to parse invalid JSON from the stream will indeed directly lead to core dumped, and this cannot be controlled with flags.
The way to avoid application interruption is to use try-catch statements to catch exceptions.

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

3 participants