cJSON

mirror of Dave's cJSON
git clone git://git.thc420.xyz/cJSON
Log | Files | Refs | README | LICENSE

commit 12b2daccf3647eaafc59364dc9d172bd336f4ff2
parent f8d0c47bdb788145afa4bd1138da559b967481c8
Author: Max Bruckner <max@maxbruckner.de>
Date:   Thu, 16 Feb 2017 01:03:38 +0100

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.

Diffstat:
McJSON.c | 21+++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/cJSON.c b/cJSON.c @@ -1029,6 +1029,7 @@ static unsigned char *print_value(const cJSON *item, size_t depth, cjbool fmt, p /* Build an array from input text. */ 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; if (*value != '[') { @@ -1044,8 +1045,8 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value, goto success; } - item->child = child = cJSON_New_Item(); - if (!item->child) + head = child = cJSON_New_Item(); + if (!child) { /* memory fail */ goto fail; @@ -1092,14 +1093,14 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value, success: item->type = cJSON_Array; + item->child = head; return value + 1; fail: - if (item->child != NULL) + if (child != NULL) { - cJSON_Delete(item->child); - item->child = NULL; + cJSON_Delete(child); } 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. */ 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; if (*value != '{') { @@ -1292,9 +1294,8 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value goto success; } - child = cJSON_New_Item(); - item->child = child; - if (!item->child) + head = child = cJSON_New_Item(); + if (!child) { goto fail; } @@ -1369,14 +1370,14 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value success: item->type = cJSON_Object; + item->child = head; return value + 1; fail: - if (item->child != NULL) + if (child != NULL) { cJSON_Delete(child); - item->child = NULL; } return NULL;