diff --git a/cJSON.c b/cJSON.c index 7518389..0acf487 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1975,15 +1975,6 @@ static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) suffix_object(child->prev, item); array->child->prev = item; } - else - { - while (child->next) - { - child = child->next; - } - suffix_object(child, item); - array->child->prev = item; - } } return true; @@ -2571,6 +2562,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) } p = n; } + a->child->prev = n; return a; } @@ -2607,6 +2599,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) } p = n; } + a->child->prev = n; return a; } @@ -2643,6 +2636,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) } p = n; } + a->child->prev = n; return a; } @@ -2679,6 +2673,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co } p = n; } + a->child->prev = n; return a; } @@ -2751,6 +2746,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) } child = child->next; } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } return newitem; diff --git a/cJSON_Utils.c b/cJSON_Utils.c index c750f7f..f4ad32a 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -403,7 +403,7 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which) /* item doesn't exist */ return NULL; } - if (c->prev) + if (c != array->child) { /* not the first element */ c->prev->next = c->next; @@ -412,10 +412,14 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which) { c->next->prev = c->prev; } - if (c==array->child) + if (c == array->child) { array->child = c->next; } + else if (c->next == NULL) + { + array->child->prev = c->prev; + } /* make sure the detached item doesn't point anywhere anymore */ c->prev = c->next = NULL;