From e9d1de24cf69e046b683fc0ae24469231e5249c7 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 27 Jun 2017 22:58:51 +0200 Subject: [PATCH] handle null pointers: cJSON_GetArraySize --- cJSON.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cJSON.c b/cJSON.c index 80d35db..499ec2e 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1653,17 +1653,25 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out /* Get Array size/item / object item. */ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) { - cJSON *c = array->child; - size_t i = 0; - while(c) + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) { - i++; - c = c->next; + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; } /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ - return (int)i; + return (int)size; } static cJSON* get_array_item(const cJSON *array, size_t index)