diff --git a/cJSON.c b/cJSON.c index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_Y0pTT04uYw==..3aa99e9a3bbb293c2b7a78c286fcc4b0bc389444_Y0pTT04uYw== 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1865,7 +1865,7 @@ { cJSON *child = NULL; - if ((item == NULL) || (array == NULL)) + if ((item == NULL) || (array == NULL) || (array == item)) { return false; } @@ -1904,5 +1904,5 @@ } /* Add item to array/object. */ -CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item) +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) { @@ -1908,5 +1908,5 @@ { - add_item_to_array(array, item); + return add_item_to_array(array, item); } #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) @@ -1930,7 +1930,7 @@ char *new_key = NULL; int new_type = cJSON_Invalid; - if ((object == NULL) || (string == NULL) || (item == NULL)) + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) { return false; } @@ -1962,5 +1962,5 @@ return add_item_to_array(object, item); } -CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) { @@ -1966,5 +1966,5 @@ { - add_item_to_object(object, string, item, &global_hooks, false); + return add_item_to_object(object, string, item, &global_hooks, false); } /* Add an item to an object with constant string as key */ @@ -1968,5 +1968,5 @@ } /* Add an item to an object with constant string as key */ -CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) { @@ -1972,4 +1972,4 @@ { - add_item_to_object(object, string, item, &global_hooks, true); + return add_item_to_object(object, string, item, &global_hooks, true); } @@ -1974,6 +1974,6 @@ } -CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) { if (array == NULL) { @@ -1977,6 +1977,6 @@ { if (array == NULL) { - return; + return cJSON_False; } @@ -1981,5 +1981,5 @@ } - add_item_to_array(array, create_reference(item, &global_hooks)); + return add_item_to_array(array, create_reference(item, &global_hooks)); } @@ -1984,6 +1984,6 @@ } -CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) { if ((object == NULL) || (string == NULL)) { @@ -1987,6 +1987,6 @@ { if ((object == NULL) || (string == NULL)) { - return; + return cJSON_False; } @@ -1991,6 +1991,6 @@ } - add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); } CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) diff --git a/cJSON.h b/cJSON.h index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_Y0pTT04uaA==..3aa99e9a3bbb293c2b7a78c286fcc4b0bc389444_Y0pTT04uaA== 100644 --- a/cJSON.h +++ b/cJSON.h @@ -221,8 +221,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); /* Append item to the specified array/object. */ -CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item); -CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before * writing to `item->string` */ @@ -226,5 +226,5 @@ /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before * writing to `item->string` */ -CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ @@ -230,6 +230,6 @@ /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ -CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); -CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); /* Remove/Detach items from Arrays/Objects. */ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 1c07922e01f51ee6057a30077fe8531bf60cfcdc_dGVzdHMvbWlzY190ZXN0cy5j..3aa99e9a3bbb293c2b7a78c286fcc4b0bc389444_dGVzdHMvbWlzY190ZXN0cy5j 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -332,9 +332,10 @@ cJSON root[1] = {{ NULL, NULL, NULL, 0, NULL, 0, 0, NULL }}; cJSON *child = NULL; cJSON *replacement = NULL; + cJSON_bool flag = cJSON_False; child = cJSON_CreateNumber(1); TEST_ASSERT_NOT_NULL(child); replacement = cJSON_CreateNumber(2); TEST_ASSERT_NOT_NULL(replacement); @@ -335,10 +336,11 @@ child = cJSON_CreateNumber(1); TEST_ASSERT_NOT_NULL(child); replacement = cJSON_CreateNumber(2); TEST_ASSERT_NOT_NULL(replacement); - cJSON_AddItemToObject(root, "child", child); + flag = cJSON_AddItemToObject(root, "child", child); + TEST_ASSERT_TRUE_MESSAGE(flag, "add item to object failed"); cJSON_ReplaceItemInObject(root, "child", replacement); TEST_ASSERT_TRUE(root->child == replacement); @@ -531,6 +533,22 @@ cJSON_Delete(number_reference); } +static void cjson_add_item_to_object_or_array_should_not_add_itself(void) +{ + cJSON *object = cJSON_CreateObject(); + cJSON *array = cJSON_CreateArray(); + cJSON_bool flag = cJSON_False; + + flag = cJSON_AddItemToObject(object, "key", object); + TEST_ASSERT_FALSE_MESSAGE(flag, "add an object to itself should fail"); + + flag = cJSON_AddItemToArray(array, array); + TEST_ASSERT_FALSE_MESSAGE(flag, "add an array to itself should fail"); + + cJSON_Delete(object); + cJSON_Delete(array); +} + static void cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased(void) { cJSON *object = cJSON_CreateObject(); @@ -607,6 +625,7 @@ RUN_TEST(cjson_create_string_reference_should_create_a_string_reference); RUN_TEST(cjson_create_object_reference_should_create_an_object_reference); RUN_TEST(cjson_create_array_reference_should_create_an_array_reference); + RUN_TEST(cjson_add_item_to_object_or_array_should_not_add_itself); RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased); RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);