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

json.exception.parse_error.101 when parsing data received over a socket #3313

Closed
sonnyparlin opened this issue Feb 2, 2022 · 3 comments
Closed
Labels
solution: invalid the issue is not related to the library

Comments

@sonnyparlin
Copy link

sonnyparlin commented Feb 2, 2022

What is the issue you have?

Getting a parse error when parsing data received via a read loop over a tcp socket.

The Error

[json.exception.parse_error.101]

Please describe the steps to reproduce the issue.

  1. Set up a tcp socket and read data from the socket stream with a read loop
  2. Set the buffer size to be smaller than the amount of data coming over the socket so the data has to be read in a loop
  3. json parse the data after it's been read.

I have a string that's 95 chars long, I set my read buffer to 55 to make sure that all data gets read via the loop. I can confirm that I receive all of the data with a std::cout of the buffer, but when I try to parse the buffer, I get a parse error. The parse function thinks it only received 55 chars. Changing the buffer size to a number larger than the message text fixes the issue. But I need to be able to read the data in a loop as I will be transferring large json dumps. My code is below:

void SocketCommunication::receive_node_message(int sock) {
    const int BUFFER_SIZE = 55; // <--- problem is here
    char buffer[BUFFER_SIZE] = {0};
    ... 
    while (offset < sizetoread) {
        ssize_t reader = recv(sock, buffer+offset, sizetoread-offset, 0); // get message
        if(reader == 0) {
            break;
        }
        offset+=reader;
    }

    std::cout << buffer << std::endl; // <--- prints the entire string (all 95 characters) shown below
    // {"Message":{"Peers":null,"SocketConnector":{"ip":"127.0.0.1","port":10002},"Type":"DISCOVERY"}}

    auto j = nlohmann::json::parse(buffer); // <--- fails with parse error

What is the expected behavior?

The json should get parsed because I'm providing a full json string to the parse() method.

And what is the actual behavior instead?

libc++abi: terminating with uncaught exception of type nlohmann::detail::parse_error:
[json.exception.parse_error.101] parse error at line 1, column 56: syntax error while parsing
value - invalid string: missing closing quote; last read: '"127.0'

Which compiler and operating system are you using?

  • Compiler: /usr/bin/g++ -Wall -std=c++17
  • Operating system: OSX 12.1

Which version of the library did you use?

3.10.5

@nlohmann
Copy link
Owner

nlohmann commented Feb 2, 2022

Indeed, the library will only read the buffer until its last character, so the error message makes sense here. Writing/reading to the buffer past its size should be undefined behavior - please run your code with Valgrind or ASAN.

@sonnyparlin
Copy link
Author

I'm not sure I understand your answer (sorry I'm new to cpp) but it would seem that receiving large amounts of data in pieces via a socket read loop is well established doctrine in terms of network programming. Also, I'm giving nlohmann::json a full string to work with. The program reads data into a variable until the loop is done and the entire message has been delivered. Only after I've received the entire message do I try to json parse it. As you can see in the example above, I'm sending the entire string to be parsed.

@sonnyparlin
Copy link
Author

After debugging, it turns out that buffer is indeed only 55 characters, I must be doing something wrong. Sorry for the trouble...

@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: bug labels Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants