From 3d00120d003e53c46559d2c66a34a4d1f12250f7 Mon Sep 17 00:00:00 2001 From: rishi-ragavan Date: Sun, 9 Aug 2026 15:24:20 -0500 Subject: [PATCH 1/2] Prevent cJSON_ParseWithLengthOpts() from setting global_error to NULL and returning. Fixes #1035 The function cJSON_ParseWithLengthOpts() didn't change global_error when returning NULL, so I added an else statement that made sure to set global_error to an empty string so it can later be dereferenced without an segementation fault error. --- cJSON.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cJSON.c b/cJSON.c index 88c2d95b..cc219231 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1219,6 +1219,12 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer global_error = local_error; } + else + { + const unsigned char *safe_to_dereference = (const unsigned char*)""; + global_error.json = safe_to_dereference; + global_error.position = 0; + } return NULL; } From 4cee111b6e1ee13fffef916a777497e8d3a9ada3 Mon Sep 17 00:00:00 2001 From: rishi-ragavan Date: Thu, 20 Aug 2026 22:34:07 -0500 Subject: [PATCH 2/2] Fix cJSON_GetErrorPtr returning NULL --- cJSON.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cJSON.c b/cJSON.c index cc219231..68a02660 100644 --- a/cJSON.c +++ b/cJSON.c @@ -92,7 +92,15 @@ typedef struct { static error global_error = { NULL, 0 }; CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) -{ +{ + if (global_error.json == NULL) + { + const unsigned char *safe_to_dereference = (const unsigned char*)""; + global_error.json = safe_to_dereference; + global_error.position = 0; + + } + return (const char*) (global_error.json + global_error.position); } @@ -1219,12 +1227,6 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer global_error = local_error; } - else - { - const unsigned char *safe_to_dereference = (const unsigned char*)""; - global_error.json = safe_to_dereference; - global_error.position = 0; - } return NULL; }