From a27c8a9b003dbb7d611b66e9e1f2a017b3fa3193 Mon Sep 17 00:00:00 2001 From: ZhaoYandong00 Date: Thu, 4 Jun 2026 16:49:40 +0800 Subject: [PATCH] Improve string handling and null checks in cJSON.c Refactor string handling in cJSON.c to improve null checks and prevent overlapping string issues. --- cJSON.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/cJSON.c b/cJSON.c index 88c2d95b..a2228be4 100644 --- a/cJSON.c +++ b/cJSON.c @@ -442,24 +442,25 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) { return NULL; } - /* return NULL if the object is corrupted or valuestring is NULL */ - if (object->valuestring == NULL || valuestring == NULL) + /* return NULL if valuestring is NULL */ + if (valuestring == NULL) { return NULL; } - - v1_len = strlen(valuestring); - v2_len = strlen(object->valuestring); - - if (v1_len <= v2_len) + if(object->valuestring) { - /* 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 )) + v1_len = strlen(valuestring); + v2_len = strlen(object->valuestring); + + if (v1_len <= v2_len) { - return NULL; + /* 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(object->valuestring, valuestring); + return object->valuestring; + } } - strcpy(object->valuestring, valuestring); - return object->valuestring; } copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); if (copy == NULL)