From 1f4a5ac78ea5098edcfd920c5f5051c3a20793e4 Mon Sep 17 00:00:00 2001 From: Kanishk Date: Mon, 9 Nov 2026 22:52:28 +0530 Subject: [PATCH 1/2] Fix undefined pointer comparison in cJSON_SetValuestring --- cJSON.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 88c2d95b3..1464c30c0 100644 --- a/cJSON.c +++ b/cJSON.c @@ -431,6 +431,35 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) return object->valuedouble = number; } +/* Returns true if the strings a and b share at least one byte (terminators included). + * Only pointer equality is used: relational comparison (<, >) of pointers into + * different objects is undefined behavior. */ + +static cJSON_bool strings_overlap(const char *a, size_t a_len, const char *b, size_t b_len) +{ + size_t i = 0; + + /* does a start at one of the bytes of b? */ + for (i = 0; i <= b_len; i++) + { + if (a == b + i) + { + return true; + } + } + + /* does b start at one of the bytes of a? */ + for (i = 0; i <= a_len; i++) + { + if (b == a + i) + { + return true; + } + } + + return false; +} + /* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) { @@ -453,8 +482,8 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) if (v1_len <= v2_len) { - /* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */ - if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring )) + /* strcpy does not handle overlapping strings */ + if (strings_overlap(valuestring, v1_len, object->valuestring, v2_len)) { return NULL; } From 8ce1f3ce99e2444be1b068eac87dc7e09cabd711 Mon Sep 17 00:00:00 2001 From: Kanishk Date: Mon, 9 Nov 2026 23:02:49 +0530 Subject: [PATCH 2/2] Edited variable names according to CONTRIBUTING.md --- cJSON.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cJSON.c b/cJSON.c index 1464c30c0..365151635 100644 --- a/cJSON.c +++ b/cJSON.c @@ -435,23 +435,23 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) * Only pointer equality is used: relational comparison (<, >) of pointers into * different objects is undefined behavior. */ -static cJSON_bool strings_overlap(const char *a, size_t a_len, const char *b, size_t b_len) +static cJSON_bool strings_overlap(const char *first_string, size_t first_string_length, const char *second_string, size_t second_string_length) { - size_t i = 0; + size_t offset = 0; /* does a start at one of the bytes of b? */ - for (i = 0; i <= b_len; i++) + for (offset = 0; offset <= second_string_length; offset++) { - if (a == b + i) + if (first_string == second_string + offset) { return true; } } /* does b start at one of the bytes of a? */ - for (i = 0; i <= a_len; i++) + for (offset = 0; offset <= first_string_length; offset++) { - if (b == a + i) + if (second_string == first_string + offset) { return true; }