mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
update cJSON_SetValuestring and testcase
This commit is contained in:
18
cJSON.c
18
cJSON.c
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user