nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

JSON can't parse a simple data #4383

Closed jakitliang closed 4 weeks ago

jakitliang commented 1 month ago

Description

Example data (dump from LLDB):

(lldb) p data
(char *) $3 = 0x000001ffa6a5a5d0 "{\n\t"version": [2, 6, 0, 109],\n\t"path": "C:\\Users\\Jakit\\Documents\\data\\data1"\n}\n"

Reproduction steps

Just parse the JSON then crashing

Crash position:

截屏2024-05-29 17 55 32 截屏2024-05-29 17 57 49 截屏2024-05-29 17 58 00

Expected vs. actual results

no crashing

Minimal code example

{
    "version": [2, 6, 0, 109],
    "path": "C:\\Users\\Jakit\\Documents\\data\\data"
}

Error messages

Critical error detected c0000374

Compiler and operating system

MSVC 2019

Library version

main\origin

Validation

nlohmann commented 1 month ago

Can you share the code with that you parse that JSON?

This works without issue:

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
    auto j = json::parse(R"({
    "version": [2, 6, 0, 109],
    "path": "C:\\Users\\Jakit\\Documents\\data\\data"
})");
    std::cout << j << std::endl;
}
jakitliang commented 4 weeks ago

Can you share the code with that you parse that JSON?

This works without issue:

#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
    auto j = json::parse(R"({
  "version": [2, 6, 0, 109],
  "path": "C:\\Users\\Jakit\\Documents\\data\\data"
})");
    std::cout << j << std::endl;
}

The JSON string was store in a file data.json.

And I had found that was my fault. I got a negative number from std::ifstream::tellg then use this number as bufferSize to create a buffer storing the JSON string.

File exists but somewhere code may not correct.

So the research result is that json::parse a dirty buffer char[1024] includes some bytes like <0x09> <0x01> would crash.

And I close this issue since I fix some my issues of the file reading and buffer caching.

Thanks much!