Skip to content

Crashing on json::at #2062

Description

@IreliaNina

Hello, I'm trying to parse json objects sent from the client to my server. I completely undertsand that the client at times might not send valid json data in case of packet manipulation but I assume if I try catch json parse and json.at then I shouldn't have any issues. But it seems when I try to catch json::at get an out of range exception.

The client is sending non valid empty json like this
{}

This is the code for checking the valid json

    message[length] = 0;
    const std::string payload(message);
    
    json packet;
    try {
        packet = json::parse(payload);
    } catch (json::parse_error& e) {
        std::cerr << e.what() << std::endl;
        return ;
    }
    uint32_t packetid = 0;
    try {
        packetid = packet.at("id");
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
        return ;
    }
    std::cout << payload << std::endl;

When I send an invalid json the code should return but instead it just crashes.
This is where it seems to be crashing.

#1 0x000000010003bfb5 in std::__1::__throw_out_of_range(char const*) at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/stdexcept:262

this std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer>, std::__1::less<void>, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer> > > > * 0x000000010082ea10 size=0 0x000000010082ea10

    0x10003bf15 <+37>: mov    rax, qword ptr [rbp - 0x20]
    0x10003bf19 <+41>: cmp    qword ptr [rax], 0x0
    0x10003bf1d <+45>: jne    0x10003bf2f               ; <+63> at map:1544:40
    0x10003bf23 <+51>: lea    rdi, [rip + 0x16e620]     ; "map::at:  key not found"
    0x10003bf2a <+58>: call   0x10003bf50               ; std::__1::__throw_out_of_range at stdexcept:260
->  0x10003bf2f <+63>: mov    rax, qword ptr [rbp - 0x20]

I'm not sure if this is a bug with the source code or I'm doing something wrong but the way I see it, in case of an invalid json the code shouldn't continue. And if it does continue then catching std::exception should prevent the crash.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: invalidthe issue is not related to the librarystate: needs more infothe author of the issue needs to provide more details

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions