Add return value checks for cJSON library (#1153)

This commit is contained in:
Wenyong Huang 2022-05-07 17:31:54 +08:00 committed by GitHub
parent 2bac6a42a7
commit 21e59d883f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 23 deletions

View File

@ -1818,9 +1818,9 @@ add_item_to_array(cJSON *array, cJSON *item)
} }
/* Add item to array/object. */ /* Add item to array/object. */
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item) CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
{ {
add_item_to_array(array, item); return add_item_to_array(array, item);
} }
#if defined(__clang__) \ #if defined(__clang__) \
@ -1878,36 +1878,38 @@ add_item_to_object(cJSON *const object, const char *const string,
return add_item_to_array(object, item); return add_item_to_array(object, item);
} }
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{ {
add_item_to_object(object, string, item, &global_hooks, false); return add_item_to_object(object, string, item, &global_hooks, false);
} }
/* Add an item to an object with constant string as key */ /* Add an item to an object with constant string as key */
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
{ {
add_item_to_object(object, string, item, &global_hooks, true); return add_item_to_object(object, string, item, &global_hooks, true);
} }
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
{ {
if (array == NULL) { if (array == NULL) {
return; return false;
} }
add_item_to_array(array, create_reference(item, &global_hooks)); return add_item_to_array(array, create_reference(item, &global_hooks));
} }
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
{ {
if ((object == NULL) || (string == NULL)) { if ((object == NULL) || (string == NULL)) {
return; return false;
} }
add_item_to_object(object, string, create_reference(item, &global_hooks), return add_item_to_object(object, string,
create_reference(item, &global_hooks),
&global_hooks, false); &global_hooks, false);
} }
@ -2093,19 +2095,18 @@ cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
} }
/* Replace array/object items with new ones. */ /* Replace array/object items with new ones. */
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
{ {
cJSON *after_inserted = NULL; cJSON *after_inserted = NULL;
if (which < 0) { if (which < 0) {
return; return false;
} }
after_inserted = get_array_item(array, (size_t)which); after_inserted = get_array_item(array, (size_t)which);
if (after_inserted == NULL) { if (after_inserted == NULL) {
add_item_to_array(array, newitem); return add_item_to_array(array, newitem);
return;
} }
newitem->next = after_inserted; newitem->next = after_inserted;
@ -2117,6 +2118,7 @@ cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
else { else {
newitem->prev->next = newitem; newitem->prev->next = newitem;
} }
return true;
} }
CJSON_PUBLIC(cJSON_bool) CJSON_PUBLIC(cJSON_bool)

View File

@ -247,20 +247,21 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count); CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
/* Append item to the specified array/object. */ /* Append item to the specified array/object. */
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item); CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
/* Use this when string is definitely const (i.e. a literal, or as good as), and /* Use this when string is definitely const (i.e. a literal, or as good as), and
* will definitely survive the cJSON object. WARNING: When this function was * will definitely survive the cJSON object. WARNING: When this function was
* used, make sure to always check that (item->type & cJSON_StringIsConst) is * used, make sure to always check that (item->type & cJSON_StringIsConst) is
* zero before writing to `item->string` */ * zero before writing to `item->string` */
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
/* Append reference to item to the specified array/object. Use this when you /* Append reference to item to the specified array/object. Use this when you
* want to add an existing cJSON to a new cJSON, but don't want to corrupt your * want to add an existing cJSON to a new cJSON, but don't want to corrupt your
* existing cJSON. */ * existing cJSON. */
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); CJSON_PUBLIC(cJSON_bool)
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool)
cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */ /* Remove/Detatch items from Arrays/Objects. */
@ -278,7 +279,7 @@ CJSON_PUBLIC(void)
cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */ /* Update array items. */
CJSON_PUBLIC(void) CJSON_PUBLIC(cJSON_bool)
cJSON_InsertItemInArray( cJSON_InsertItemInArray(
cJSON *array, int which, cJSON *array, int which,
cJSON *newitem); /* Shifts pre-existing items to the right. */ cJSON *newitem); /* Shifts pre-existing items to the right. */