|
| 1 | +From 60ff122ef5862d04b39b150541459e7f5e35add8 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Lee <peterlee@apache.org> |
| 3 | +Date: Mon, 18 Dec 2023 11:47:52 +0800 |
| 4 | +Subject: [PATCH] add NULL checkings (#809) |
| 5 | + |
| 6 | +* add NULL checks in cJSON_SetValuestring |
| 7 | + |
| 8 | +Fixes #803(CVE-2023-50472) |
| 9 | + |
| 10 | +* add NULL check in cJSON_InsertItemInArray |
| 11 | + |
| 12 | +Fixes #802(CVE-2023-50471) |
| 13 | + |
| 14 | +* add tests for NULL checks |
| 15 | + |
| 16 | +add tests for NULL checks in cJSON_InsertItemInArray and cJSON_SetValuestring |
| 17 | +--- |
| 18 | + binutils/cJSON.c | 14 ++++++++++++-- |
| 19 | + tests/misc_tests.c | 21 +++++++++++++++++++++ |
| 20 | + 2 files changed, 33 insertions(+), 2 deletions(-) |
| 21 | + |
| 22 | +diff --git a/binutils/cJSON.c b/binutils/cJSON.c |
| 23 | +index f6dd11c..faa3e29 100644 |
| 24 | +--- a/binutils/cJSON.c |
| 25 | ++++ b/binutils/cJSON.c |
| 26 | +@@ -401,7 +401,12 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) |
| 27 | + { |
| 28 | + char *copy = NULL; |
| 29 | + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ |
| 30 | +- if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference)) |
| 31 | ++ if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) |
| 32 | ++ { |
| 33 | ++ return NULL; |
| 34 | ++ } |
| 35 | ++ /* return NULL if the object is corrupted */ |
| 36 | ++ if (object->valuestring == NULL) |
| 37 | + { |
| 38 | + return NULL; |
| 39 | + } |
| 40 | +@@ -2264,7 +2269,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON |
| 41 | + { |
| 42 | + cJSON *after_inserted = NULL; |
| 43 | + |
| 44 | +- if (which < 0) |
| 45 | ++ if (which < 0 || newitem == NULL) |
| 46 | + { |
| 47 | + return false; |
| 48 | + } |
| 49 | +@@ -2275,6 +2280,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON |
| 50 | + return add_item_to_array(array, newitem); |
| 51 | + } |
| 52 | + |
| 53 | ++ if (after_inserted != array->child && after_inserted->prev == NULL) { |
| 54 | ++ /* return false if after_inserted is a corrupted array item */ |
| 55 | ++ return false; |
| 56 | ++ } |
| 57 | ++ |
| 58 | + newitem->next = after_inserted; |
| 59 | + newitem->prev = after_inserted->prev; |
| 60 | + after_inserted->prev = newitem; |
| 61 | + |
| 62 | +-- |
| 63 | +2.17.1 |
0 commit comments