Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a78d975

Browse files
vwvwAlanscut
authored andcommitted
cJSON_DetachItemViaPointer: added test and fix for check for null in item->prev
1 parent f28a468 commit a78d975

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

‎cJSON.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c
22042204

22052205
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
22062206
{
2207-
if ((parent == NULL) || (parent->child == NULL) || (item ==NULL) || (item->prev == NULL))
2207+
if ((parent == NULL) || (item == NULL) || (item !=parent->child&&item->prev == NULL))
22082208
{
22092209
return NULL;
22102210
}

‎tests/misc_tests.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ static void cjson_detach_item_via_pointer_should_detach_items(void)
280280
TEST_ASSERT_NULL_MESSAGE(parent->child, "Child of the parent wasn't set to NULL.");
281281
}
282282

283+
static void cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null(void)
284+
{
285+
cJSON list[2];
286+
cJSON parent[1];
287+
288+
memset(list, '0円', sizeof(list));
289+
290+
/* link the list */
291+
list[0].next = &(list[1]);
292+
293+
parent->child = &list[0];
294+
TEST_ASSERT_NULL_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[1])), "Failed to detach in the middle.");
295+
TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[0])) == &(list[0]), "Failed to detach in the middle.");
296+
}
297+
283298
static void cjson_replace_item_via_pointer_should_replace_items(void)
284299
{
285300
cJSON replacements[3];
@@ -746,6 +761,7 @@ int CJSON_CDECL main(void)
746761
RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons);
747762
RUN_TEST(cjson_set_number_value_should_set_numbers);
748763
RUN_TEST(cjson_detach_item_via_pointer_should_detach_items);
764+
RUN_TEST(cjson_detach_item_via_pointer_should_return_null_if_item_prev_is_null);
749765
RUN_TEST(cjson_replace_item_via_pointer_should_replace_items);
750766
RUN_TEST(cjson_replace_item_in_object_should_preserve_name);
751767
RUN_TEST(cjson_functions_should_not_crash_with_null_pointers);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /