From 13ba24fe55873e2e74adbb90f2813748542dfecd Mon Sep 17 00:00:00 2001 From: hopper-vul Date: Mon, 19 Dec 2022 19:29:17 +0800 Subject: [PATCH] Fix a null pointer crash in cJSON_ReplaceItemViaPointer If the parent passed in cJSON_ReplaceItemViaPointer has not a child, which means parent->child is null, a null pointer dereference crash will be happened inside cJSON_ReplaceItemViaPointer. This commit adds the NULL check of `parent->child` beforehand to inform user such incorrect usage. Signed-off-by: hopper-vul --- cJSON.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index 524ba46..d7aeecd 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2291,7 +2291,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) { - if ((parent == NULL) || (replacement == NULL) || (item == NULL)) + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) { return false; }