From 3facca479249986256804a29c64d15dd19c38cee Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Wed, 15 Feb 2017 16:15:15 +0100 Subject: [PATCH] parse functions: Only set type after successful This sets the type of an item only if parsing was successful. This means that in case of failure, the item's type will remain to be cJSON_Invalid. --- cJSON.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/cJSON.c b/cJSON.c index 7d2c690..0f861c5 100644 --- a/cJSON.c +++ b/cJSON.c @@ -468,7 +468,6 @@ static const unsigned char *parse_string(cJSON *item, const unsigned char *str, { goto fail; } - item->type = cJSON_String; ptr = str + 1; ptr2 = out; @@ -607,6 +606,7 @@ static const unsigned char *parse_string(cJSON *item, const unsigned char *str, ptr++; } + item->type = cJSON_String; item->valuestring = (char*)out; return ptr; @@ -1054,12 +1054,11 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value, goto fail; } - item->type = cJSON_Array; value = skip(value + 1); if (*value == ']') { /* empty array. */ - return value + 1; + goto success; } item->child = child = cJSON_New_Item(); @@ -1101,11 +1100,17 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value, if (*value == ']') { /* end of array */ - return value + 1; + goto success; } /* malformed. */ *ep = value; + goto fail; + +success: + item->type = cJSON_Array; + + return value + 1; fail: if (item->child != NULL) @@ -1297,12 +1302,11 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value goto fail; } - item->type = cJSON_Object; value = skip(value + 1); if (*value == '}') { /* empty object. */ - return value + 1; + goto success; } child = cJSON_New_Item(); @@ -1373,11 +1377,17 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value /* end of object */ if (*value == '}') { - return value + 1; + goto success; } /* malformed */ *ep = value; + goto fail; + +success: + item->type = cJSON_Object; + + return value + 1; fail: if (item->child != NULL)