From 860ed3a77c24697c3a7d796f1e9c10cedccda217 Mon Sep 17 00:00:00 2001 From: ZhaoYandong00 Date: Thu, 6 Apr 2023 15:15:04 +0800 Subject: [PATCH] fix strlen(null) When used in this way, it may cause errors : strlen(null) cJSON* json= cJSON_CreateObject(); json->type = cJSON_String; cJSON_SetValuestring(json,string); --- cJSON.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cJSON.c b/cJSON.c index 524ba46..edae4b4 100644 --- a/cJSON.c +++ b/cJSON.c @@ -405,10 +405,13 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) { return NULL; } - if (strlen(valuestring) <= strlen(object->valuestring)) + if (object->valuestring != NULL) { - strcpy(object->valuestring, valuestring); - return object->valuestring; + if (strlen(valuestring) <= strlen(object->valuestring)) + { + strcpy(object->valuestring, valuestring); + return object->valuestring; + } } copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); if (copy == NULL)