parse_{object,array}: set child only after parsing

This only attaches the parsed linked lists to the items passed to
parse_object and parse_array.
This commit is contained in:
Max Bruckner 2017-02-16 01:03:38 +01:00
parent f8d0c47bdb
commit 12b2daccf3
1 changed files with 11 additions and 10 deletions

21
cJSON.c
View File

@ -1029,6 +1029,7 @@ static unsigned char *print_value(const cJSON *item, size_t depth, cjbool fmt, p
/* Build an array from input text. */ /* Build an array from input text. */
static const unsigned char *parse_array(cJSON *item, const unsigned char *value, const unsigned char **ep) static const unsigned char *parse_array(cJSON *item, const unsigned char *value, const unsigned char **ep)
{ {
cJSON *head = NULL; /* head of the linked list */
cJSON *child = NULL; cJSON *child = NULL;
if (*value != '[') if (*value != '[')
{ {
@ -1044,8 +1045,8 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value,
goto success; goto success;
} }
item->child = child = cJSON_New_Item(); head = child = cJSON_New_Item();
if (!item->child) if (!child)
{ {
/* memory fail */ /* memory fail */
goto fail; goto fail;
@ -1092,14 +1093,14 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value,
success: success:
item->type = cJSON_Array; item->type = cJSON_Array;
item->child = head;
return value + 1; return value + 1;
fail: fail:
if (item->child != NULL) if (child != NULL)
{ {
cJSON_Delete(item->child); cJSON_Delete(child);
item->child = NULL;
} }
return NULL; return NULL;
@ -1277,6 +1278,7 @@ static unsigned char *print_array(const cJSON *item, size_t depth, cjbool fmt, p
/* Build an object from the text. */ /* Build an object from the text. */
static const unsigned char *parse_object(cJSON *item, const unsigned char *value, const unsigned char **ep) static const unsigned char *parse_object(cJSON *item, const unsigned char *value, const unsigned char **ep)
{ {
cJSON *head = NULL; /* linked list head */
cJSON *child = NULL; cJSON *child = NULL;
if (*value != '{') if (*value != '{')
{ {
@ -1292,9 +1294,8 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value
goto success; goto success;
} }
child = cJSON_New_Item(); head = child = cJSON_New_Item();
item->child = child; if (!child)
if (!item->child)
{ {
goto fail; goto fail;
} }
@ -1369,14 +1370,14 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value
success: success:
item->type = cJSON_Object; item->type = cJSON_Object;
item->child = head;
return value + 1; return value + 1;
fail: fail:
if (item->child != NULL) if (child != NULL)
{ {
cJSON_Delete(child); cJSON_Delete(child);
item->child = NULL;
} }
return NULL; return NULL;