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);
This commit is contained in:
ZhaoYandong00 2023-04-06 15:15:04 +08:00 committed by GitHub
parent b45f48e600
commit 860ed3a77c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -405,11 +405,14 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{ {
return NULL; return NULL;
} }
if (object->valuestring != NULL)
{
if (strlen(valuestring) <= strlen(object->valuestring)) if (strlen(valuestring) <= strlen(object->valuestring))
{ {
strcpy(object->valuestring, valuestring); strcpy(object->valuestring, valuestring);
return object->valuestring; return object->valuestring;
} }
}
copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
if (copy == NULL) if (copy == NULL)
{ {