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.
This commit is contained in:
Max Bruckner 2017-02-15 16:15:15 +01:00
parent cf48ea8175
commit 3facca4792
1 changed files with 17 additions and 7 deletions

24
cJSON.c
View File

@ -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)