Skip to content

Crash when parsing nullptr #4485

@zerocukor287

Description

@zerocukor287

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions