From 105eae42a9f623520d94b2d1dbd7a728855b9f4f Mon Sep 17 00:00:00 2001 From: Logan Buth Date: Sat, 18 May 2024 11:44:04 -0500 Subject: [PATCH 1/3] Copy object item key to replacement in ReplaceItemViaPointer --- cJSON.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/cJSON.c b/cJSON.c index cac1164b..5eb7abc3 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2327,6 +2327,22 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON replacement->next = item->next; replacement->prev = item->prev; + if (item->string) + { + /* duplicate name from source to replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)item->string, &global_hooks); + if (replacement->string == NULL) + { + return false; + } + + replacement->type &= ~cJSON_StringIsConst; + } + if (replacement->next != NULL) { replacement->next->prev = replacement; @@ -2378,19 +2394,6 @@ static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSO 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->type &= ~cJSON_StringIsConst; - return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); } From ddb7df07c8989709d5317804e0e1f5b480b9e10f Mon Sep 17 00:00:00 2001 From: Logan Buth Date: Sat, 18 May 2024 21:34:48 -0500 Subject: [PATCH 2/3] Add ReplaceItemViaPointer update to tests --- tests/misc_tests.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 94dd91aa..fed75858 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -345,6 +345,16 @@ static void cjson_replace_item_in_object_should_preserve_name(void) TEST_ASSERT_TRUE(root->child == replacement); TEST_ASSERT_EQUAL_STRING("child", replacement->string); + // now test the same, but with replaceitemviapointer + child = cJSON_GetObjectItemCaseSensitive(root, "child"); + TEST_ASSERT_NOT_NULL(child); + replacement = cJSON_CreateNumber(3); + TEST_ASSERT_NOT_NULL(replacement); + cJSON_ReplaceItemViaPointer(root, child, replacement); + + TEST_ASSERT_TRUE(root->child == replacement); + TEST_ASSERT_EQUAL_STRING("child", replacement->string); + cJSON_Delete(replacement); } From a6903202ca52aa734e2b89bb42536b2be69ab3c9 Mon Sep 17 00:00:00 2001 From: Logan Buth Date: Wed, 22 May 2024 22:58:59 -0500 Subject: [PATCH 3/3] Switch coment from C++ to C style Resolves CI issues --- tests/misc_tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/misc_tests.c b/tests/misc_tests.c index fed75858..c444614f 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -345,7 +345,7 @@ static void cjson_replace_item_in_object_should_preserve_name(void) TEST_ASSERT_TRUE(root->child == replacement); TEST_ASSERT_EQUAL_STRING("child", replacement->string); - // now test the same, but with replaceitemviapointer + /* now test the same, but with replaceitemviapointer */ child = cJSON_GetObjectItemCaseSensitive(root, "child"); TEST_ASSERT_NOT_NULL(child); replacement = cJSON_CreateNumber(3);