Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -2426,25 +2426,30 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON

static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
{
char *new_key = NULL;

if ((replacement == NULL) || (string == NULL))
{
return false;
}

/* duplicate the key before freeing the old one: string may alias replacement->string */
new_key = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
if (new_key == NULL)
{
return false;
}

/* replace the name in the replacement */
if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
{
cJSON_free(replacement->string);
}
replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
if (replacement->string == NULL)
{
return false;
}
replacement->string = new_key;

replacement->type &= ~cJSON_StringIsConst;

return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
return cJSON_ReplaceItemViaPointer(object, get_object_item(object, new_key, case_sensitive), replacement);
}

CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
Expand Down