From 93a79ea2c8db8c78d752a6cf0ddfcf984636b585 Mon Sep 17 00:00:00 2001 From: Super User Date: Mon, 25 Nov 2024 14:11:28 +0800 Subject: [PATCH] Fix to backport check for null in cJSON_DetachItemViaPointer --- ...r-null-in-cJSON_DetachItemViaPointer.patch | 82 +++++++++++++++++++ cjson.spec | 9 +- 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 backport-check-for-null-in-cJSON_DetachItemViaPointer.patch diff --git a/backport-check-for-null-in-cJSON_DetachItemViaPointer.patch b/backport-check-for-null-in-cJSON_DetachItemViaPointer.patch new file mode 100644 index 0000000..c1b95d7 --- /dev/null +++ b/backport-check-for-null-in-cJSON_DetachItemViaPointer.patch @@ -0,0 +1,82 @@ +From 67d170616be49c4204605802d63c2b4c9dab8c3b Mon Sep 17 00:00:00 2001 +From: Nicolas Badoux +Date: Fri, 23 Aug 2024 14:14:10 +0000 +Subject: [PATCH 1/2] Check for NULL in cJSON_DetachItemViaPointer + +--- + cJSON.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/cJSON.c b/cJSON.c +index cac1164b..483d0c0c 100644 +--- a/cJSON.c ++++ b/cJSON.c +@@ -2204,7 +2204,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c + + CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) + { +- if ((parent == NULL) || (item == NULL)) ++ if ((parent == NULL) || (parent->child == NULL) || (item == NULL) || (item->prev == NULL)) + { + return NULL; + } + +From 81b36279faf0143f99150974fca7cfc534e1798b Mon Sep 17 00:00:00 2001 +From: Nicolas Badoux +Date: Sun, 25 Aug 2024 23:18:14 +0200 +Subject: [PATCH 2/2] cJSON_DetachItemViaPointer: added test and fix for check + for null in item->prev + +--- + cJSON.c | 2 +- + tests/misc_tests.c | 16 ++++++++++++++++ + 2 files changed, 17 insertions(+), 1 deletion(-) + +diff --git a/cJSON.c b/cJSON.c +index 483d0c0c..fe22bd83 100644 +--- a/cJSON.c ++++ b/cJSON.c +@@ -2204,7 +2204,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c + + CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) + { +- if ((parent == NULL) || (parent->child == NULL) || (item == NULL) || (item->prev == NULL)) ++ if ((parent == NULL) || (item == NULL) || (item != parent->child && item->prev == NULL)) + { + return NULL; + } +diff --git a/tests/misc_tests.c b/tests/misc_tests.c +index ba3e003e..b9c59e71 100644 +--- a/tests/misc_tests.c ++++ b/tests/misc_tests.c +@@ -280,6 +280,21 @@ static void cjson_detach_item_via_pointer_should_detach_items(void) + TEST_ASSERT_NULL_MESSAGE(parent->child, "Child of the parent wasn't set to NULL."); + } + ++static void cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null(void) ++{ ++ cJSON list[2]; ++ cJSON parent[1]; ++ ++ memset(list, '\0', sizeof(list)); ++ ++ /* link the list */ ++ list[0].next = &(list[1]); ++ ++ parent->child = &list[0]; ++ TEST_ASSERT_NULL_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[1])), "Failed to detach in the middle."); ++ TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[0])) == &(list[0]), "Failed to detach in the middle."); ++} ++ + static void cjson_replace_item_via_pointer_should_replace_items(void) + { + cJSON replacements[3]; +@@ -746,6 +761,7 @@ int CJSON_CDECL main(void) + RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons); + RUN_TEST(cjson_set_number_value_should_set_numbers); + RUN_TEST(cjson_detach_item_via_pointer_should_detach_items); ++ RUN_TEST(cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null); + RUN_TEST(cjson_replace_item_via_pointer_should_replace_items); + RUN_TEST(cjson_replace_item_in_object_should_preserve_name); + RUN_TEST(cjson_functions_should_not_crash_with_null_pointers); + diff --git a/cjson.spec b/cjson.spec index a02bb38..9f9d93f 100644 --- a/cjson.spec +++ b/cjson.spec @@ -1,12 +1,14 @@ Name: cjson Version: 1.7.18 -Release: 2 +Release: 3 Summary: Ultralightweight JSON parser in ANSI C License: MIT URL: https://github.com/DaveGamble/cJSON Source0: https://github.com/DaveGamble/cJSON/archive/refs/tags/v%{version}.tar.gz - + +Patch1001: backport-check-for-null-in-cJSON_DetachItemViaPointer.patch + BuildRequires: gcc BuildRequires: cmake @@ -45,6 +47,9 @@ developing applications that use cJSON. %{_includedir}/cjson/ %changelog +* Fri Nov 22 2024 xiejing - 1.7.18-3 +- Fix to check for null in cJSON_DetachItemViaPointer + * Mon Nov 04 2024 Funda Wang - 1.7.18-2 - adopt to new cmake macro -- Gitee