-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Description
Description
A few days ago I had a crash, and spent a huge amount of time debugging my code, when I realized that if I call the json::parse()
function with nullptr
, the whole shebang go up in flames with the message: "Access violation reading location 0x00000000" in input_adapters.hpp line 449.
Reproduction steps
It's quite simple, call the json::parse
function with nullptr
. Reproducible on
- Ubuntu 20.04.6 LTS with gcc 9.4.0 and json 3.9.1
- Win 11, MSVC Version 19.41.34120 and debug branch
Expected vs. actual results
Based on the documentations, I would expect an exception (unless someone explicitly turns those off, but it is not the case here).
In reality, we receive a SIGSEV, or Access violation depending on the platform.
See:
input_adapters.hpp
line 449
...
contiguous_bytes_input_adapter input_adapter(CharT b)
{
auto length = std::strlen(reinterpret_cast<const char*>(b));
Exception has occurred: W32/0xC0000005
Unhandled exception at 0x690392C0 (ucrtbased.dll) in test.exe.exe: 0xC0000005: Access violation reading location 0x00000000.
Minimal code example
#include "nlohmann/json.hpp"
#include <iostream>
int main(int argc, char* argv[]) {
const char * string = nullptr;
try {
auto result = nlohmann::json::parse(string);
} catch(...) {
std::cout << "Exception caught, no harm." << std::endl;
}
std::cout << "All good, nothing to see." << std::endl;
return 0;
}
Error messages
Exception has occurred: W32/0xC0000005
Unhandled exception at 0x690392C0 (ucrtbased.dll) in test.exe.exe: 0xC0000005: Access violation reading location 0x00000000.
Compiler and operating system
Ubuntu 20.04.6 LTS with gcc 9.4.0
Library version
3.9.1
Validation
- The bug also occurs if the latest version from the
develop
branch is used. - I can successfully compile and run the unit tests.