Skip to content

Commit

Permalink
add NULL check in cJSON_InsertItemInArray
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterAlfredLee committed Dec 16, 2023
1 parent 276a462 commit 775c807
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON
{
cJSON *after_inserted = NULL;

if (which < 0)
if (which < 0 || newitem == NULL)
{
return false;
}
Expand All @@ -2280,6 +2280,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON
return add_item_to_array(array, newitem);
}

if (after_inserted != array->child && newitem->prev == NULL) {
/* return false if after_inserted is a corrupted array item */
return false;
}

newitem->next = after_inserted;
newitem->prev = after_inserted->prev;
after_inserted->prev = newitem;
Expand Down

0 comments on commit 775c807

Please sign in to comment.