Kaynağa Gözat

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.
Max Bruckner 8 yıl önce
ebeveyn
işleme
3facca4792
1 değiştirilmiş dosya ile 17 ekleme ve 7 silme
  1. 17 7
      cJSON.c

+ 17 - 7
cJSON.c

@@ -468,7 +468,6 @@ static const unsigned char *parse_string(cJSON *item, const unsigned char *str,
     {
     {
         goto fail;
         goto fail;
     }
     }
-    item->type = cJSON_String;
 
 
     ptr = str + 1;
     ptr = str + 1;
     ptr2 = out;
     ptr2 = out;
@@ -607,6 +606,7 @@ static const unsigned char *parse_string(cJSON *item, const unsigned char *str,
         ptr++;
         ptr++;
     }
     }
 
 
+    item->type = cJSON_String;
     item->valuestring = (char*)out;
     item->valuestring = (char*)out;
 
 
     return ptr;
     return ptr;
@@ -1054,12 +1054,11 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value,
         goto fail;
         goto fail;
     }
     }
 
 
-    item->type = cJSON_Array;
     value = skip(value + 1);
     value = skip(value + 1);
     if (*value == ']')
     if (*value == ']')
     {
     {
         /* empty array. */
         /* empty array. */
-        return value + 1;
+        goto success;
     }
     }
 
 
     item->child = child = cJSON_New_Item();
     item->child = child = cJSON_New_Item();
@@ -1101,11 +1100,17 @@ static const unsigned char *parse_array(cJSON *item, const unsigned char *value,
     if (*value == ']')
     if (*value == ']')
     {
     {
         /* end of array */
         /* end of array */
-        return value + 1;
+        goto success;
     }
     }
 
 
     /* malformed. */
     /* malformed. */
     *ep = value;
     *ep = value;
+    goto fail;
+
+success:
+    item->type = cJSON_Array;
+
+    return value + 1;
 
 
 fail:
 fail:
     if (item->child != NULL)
     if (item->child != NULL)
@@ -1297,12 +1302,11 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value
         goto fail;
         goto fail;
     }
     }
 
 
-    item->type = cJSON_Object;
     value = skip(value + 1);
     value = skip(value + 1);
     if (*value == '}')
     if (*value == '}')
     {
     {
         /* empty object. */
         /* empty object. */
-        return value + 1;
+        goto success;
     }
     }
 
 
     child = cJSON_New_Item();
     child = cJSON_New_Item();
@@ -1373,11 +1377,17 @@ static const unsigned char *parse_object(cJSON *item, const unsigned char *value
     /* end of object */
     /* end of object */
     if (*value == '}')
     if (*value == '}')
     {
     {
-        return value + 1;
+        goto success;
     }
     }
 
 
     /* malformed */
     /* malformed */
     *ep = value;
     *ep = value;
+    goto fail;
+
+success:
+    item->type = cJSON_Object;
+
+    return value + 1;
 
 
 fail:
 fail:
     if (item->child != NULL)
     if (item->child != NULL)