update cJSON_SetValuestring and testcase

This commit is contained in:
Alanscut
2020-03-25 15:38:54 +08:00
parent 34e102d0dc
commit 31c7880fab
4 changed files with 36 additions and 24 deletions

18
cJSON.c
View File

@ -368,23 +368,25 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
return object->valuedouble = number;
}
CJSON_PUBLIC(char*) cJSON_SetValuestringToObject(cJSON *object, const char *valuestring)
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{
size_t length = 0;
char *copy = NULL;
/* if object->valuestring is NULL, it should not set valuestring */
if (object->valuestring == NULL)
/* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference))
{
return NULL;
}
length = strlen(valuestring) + sizeof("");
copy = (char*) cJSON_malloc(length);
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)
{
return NULL;
}
memcpy(copy, valuestring, length);
if (!(object->type & cJSON_IsReference) && (object->valuestring != NULL))
if (object->valuestring != NULL)
{
cJSON_free(object->valuestring);
}