]> git.feebdaed.xyz Git - 0xmirror/cJSON.git/commitdiff
Fix a null pointer crash in cJSON_ReplaceItemViaPointer (#726)
authorhopper-vul <118949689+hopper-vul@users.noreply.github.com>
Sat, 1 Jul 2023 08:18:32 +0000 (16:18 +0800)
committerGitHub <noreply@github.com>
Sat, 1 Jul 2023 08:18:32 +0000 (16:18 +0800)
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 <hopper.vul@gmail.com>
cJSON.c

diff --git a/cJSON.c b/cJSON.c
index 524ba4641145c56d24ffb361ef86d657e7c259e1..d7aeecd9ea757aa3f9b84c1d9fa3d8adf256e925 100644 (file)
--- 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;
     }