cJSON
cJSON copied to clipboard
reject valid input when parsing json text
Hi, we have encountered an error when attempting to parse a JSON text using the parser. However, we have checked the text against the JSON grammar specified in RFC 7159 and found it to be compliant (i.e., JSON parser MUST accept all texts that conform to the JSON grammar). Other JSON parsers, such as those found at jsonlint.com and github.com/nlohmann/json, were able to parse the text without issue.
Specifically, we use the following code to parse a json text:
const char *s = "{\"a\": true, \"b\": [ null,9999999999999999999999999999999999999999999999912345678901234567]}";
cJSON *root = NULL;
root = cJSON_Parse(s);
if (root == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
printf("error in json data:%s\n", error_ptr);
}
#include <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>
int main(void) {
// JSON string with an extremely large number
const char *s = "{\"a\": true, \"b\": [ null,9999999999999999999999999999999999999999999999912345678901234567]}";
// Attempt to parse the JSON string
cJSON *root = cJSON_Parse(s);
// Check for parsing errors
if (root == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
printf("Error parsing JSON at: %s\n", error_ptr);
} else {
printf("Error: cJSON returned NULL without specifying an error pointer.\n");
}
return EXIT_FAILURE;
}
// Successfully parsed JSON
printf("Successfully parsed JSON:\n%s\n", cJSON_Print(root));
// Cleanup
cJSON_Delete(root);
return EXIT_SUCCESS;
}