|
@@ -1111,6 +1111,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON
|
|
|
|
|
|
if (!print_value(item, &p))
|
|
if (!print_value(item, &p))
|
|
{
|
|
{
|
|
|
|
+ global_hooks.deallocate(p.buffer);
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1121,7 +1122,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const i
|
|
{
|
|
{
|
|
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
|
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
|
|
|
|
|
|
- if (len < 0)
|
|
|
|
|
|
+ if ((len < 0) || (buf == NULL))
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -1652,17 +1653,25 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out
|
|
/* Get Array size/item / object item. */
|
|
/* Get Array size/item / object item. */
|
|
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
|
|
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
|
|
{
|
|
{
|
|
- cJSON *c = array->child;
|
|
|
|
- size_t i = 0;
|
|
|
|
- while(c)
|
|
|
|
|
|
+ cJSON *child = NULL;
|
|
|
|
+ size_t size = 0;
|
|
|
|
+
|
|
|
|
+ if (array == NULL)
|
|
{
|
|
{
|
|
- i++;
|
|
|
|
- c = c->next;
|
|
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ child = array->child;
|
|
|
|
+
|
|
|
|
+ while(child != NULL)
|
|
|
|
+ {
|
|
|
|
+ size++;
|
|
|
|
+ child = child->next;
|
|
}
|
|
}
|
|
|
|
|
|
/* FIXME: Can overflow here. Cannot be fixed without breaking the API */
|
|
/* FIXME: Can overflow here. Cannot be fixed without breaking the API */
|
|
|
|
|
|
- return (int)i;
|
|
|
|
|
|
+ return (int)size;
|
|
}
|
|
}
|
|
|
|
|
|
static cJSON* get_array_item(const cJSON *array, size_t index)
|
|
static cJSON* get_array_item(const cJSON *array, size_t index)
|
|
@@ -1747,16 +1756,23 @@ static void suffix_object(cJSON *prev, cJSON *item)
|
|
/* Utility for handling references. */
|
|
/* Utility for handling references. */
|
|
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
|
|
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
|
|
{
|
|
{
|
|
- cJSON *ref = cJSON_New_Item(hooks);
|
|
|
|
- if (!ref)
|
|
|
|
|
|
+ cJSON *reference = NULL;
|
|
|
|
+ if (item == NULL)
|
|
{
|
|
{
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
- memcpy(ref, item, sizeof(cJSON));
|
|
|
|
- ref->string = NULL;
|
|
|
|
- ref->type |= cJSON_IsReference;
|
|
|
|
- ref->next = ref->prev = NULL;
|
|
|
|
- return ref;
|
|
|
|
|
|
+
|
|
|
|
+ reference = cJSON_New_Item(hooks);
|
|
|
|
+ if (reference == NULL)
|
|
|
|
+ {
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ memcpy(reference, item, sizeof(cJSON));
|
|
|
|
+ reference->string = NULL;
|
|
|
|
+ reference->type |= cJSON_IsReference;
|
|
|
|
+ reference->next = reference->prev = NULL;
|
|
|
|
+ return reference;
|
|
}
|
|
}
|
|
|
|
|
|
/* Add item to array/object. */
|
|
/* Add item to array/object. */
|
|
@@ -1789,6 +1805,11 @@ CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
|
|
|
|
|
|
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
|
|
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
|
|
{
|
|
{
|
|
|
|
+ if (item == NULL)
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
/* call cJSON_AddItemToObjectCS for code reuse */
|
|
/* call cJSON_AddItemToObjectCS for code reuse */
|
|
cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
|
|
cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
|
|
/* remove cJSON_StringIsConst flag */
|
|
/* remove cJSON_StringIsConst flag */
|
|
@@ -1805,7 +1826,7 @@ CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSO
|
|
/* Add an item to an object with constant string as key */
|
|
/* Add an item to an object with constant string as key */
|
|
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
|
|
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
|
|
{
|
|
{
|
|
- if (!item)
|
|
|
|
|
|
+ if ((item == NULL) || (string == NULL))
|
|
{
|
|
{
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -1823,11 +1844,21 @@ CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJ
|
|
|
|
|
|
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
|
|
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
|
|
{
|
|
{
|
|
|
|
+ if (array == NULL)
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
|
|
cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
|
|
}
|
|
}
|
|
|
|
|
|
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
|
|
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
|
|
{
|
|
{
|
|
|
|
+ if ((object == NULL) || (string == NULL))
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
|
|
cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1932,7 +1963,7 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newit
|
|
|
|
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
|
|
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
|
|
{
|
|
{
|
|
- if ((parent == NULL) || (replacement == NULL))
|
|
|
|
|
|
+ if ((parent == NULL) || (replacement == NULL) || (item == NULL))
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -1977,7 +2008,7 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newi
|
|
|
|
|
|
static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
|
|
static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
|
|
{
|
|
{
|
|
- if (replacement == NULL)
|
|
|
|
|
|
+ if ((replacement == NULL) || (string == NULL))
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -2140,7 +2171,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
|
|
cJSON *p = NULL;
|
|
cJSON *p = NULL;
|
|
cJSON *a = NULL;
|
|
cJSON *a = NULL;
|
|
|
|
|
|
- if (count < 0)
|
|
|
|
|
|
+ if ((count < 0) || (numbers == NULL))
|
|
{
|
|
{
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
@@ -2175,7 +2206,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
|
|
cJSON *p = NULL;
|
|
cJSON *p = NULL;
|
|
cJSON *a = NULL;
|
|
cJSON *a = NULL;
|
|
|
|
|
|
- if (count < 0)
|
|
|
|
|
|
+ if ((count < 0) || (numbers == NULL))
|
|
{
|
|
{
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
@@ -2211,7 +2242,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
|
|
cJSON *p = NULL;
|
|
cJSON *p = NULL;
|
|
cJSON *a = NULL;
|
|
cJSON *a = NULL;
|
|
|
|
|
|
- if (count < 0)
|
|
|
|
|
|
+ if ((count < 0) || (numbers == NULL))
|
|
{
|
|
{
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
@@ -2247,7 +2278,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
|
|
cJSON *p = NULL;
|
|
cJSON *p = NULL;
|
|
cJSON *a = NULL;
|
|
cJSON *a = NULL;
|
|
|
|
|
|
- if (count < 0)
|
|
|
|
|
|
+ if ((count < 0) || (strings == NULL))
|
|
{
|
|
{
|
|
return NULL;
|
|
return NULL;
|
|
}
|
|
}
|
|
@@ -2359,6 +2390,12 @@ fail:
|
|
CJSON_PUBLIC(void) cJSON_Minify(char *json)
|
|
CJSON_PUBLIC(void) cJSON_Minify(char *json)
|
|
{
|
|
{
|
|
unsigned char *into = (unsigned char*)json;
|
|
unsigned char *into = (unsigned char*)json;
|
|
|
|
+
|
|
|
|
+ if (json == NULL)
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
while (*json)
|
|
while (*json)
|
|
{
|
|
{
|
|
if (*json == ' ')
|
|
if (*json == ' ')
|