From 3999b12848214a201bf56ef416b9dac1ec63f811 Mon Sep 17 00:00:00 2001 From: miaoerduo Date: Fri, 3 Apr 2020 11:52:54 +0800 Subject: [PATCH 1/3] feat: set list head's prev in parse_array and parse_object --- cJSON.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cJSON.c b/cJSON.c index a5d3987..500fcf5 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1509,6 +1509,10 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf success: input_buffer->depth--; + if (head != NULL) { + head->prev = current_item; + } + item->type = cJSON_Array; item->child = head; @@ -1681,6 +1685,10 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu success: input_buffer->depth--; + if (head != NULL) { + head->prev = current_item; + } + item->type = cJSON_Object; item->child = head; From a65abf2f4f1842765761a616e93c14ae25a284fd Mon Sep 17 00:00:00 2001 From: miaoerduo Date: Fri, 3 Apr 2020 14:07:22 +0800 Subject: [PATCH 2/3] fix: error list head's prev when detach the last item --- cJSON.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cJSON.c b/cJSON.c index 500fcf5..cc1c016 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2210,6 +2210,12 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it /* first element */ parent->child = item->next; } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + /* make sure the detached item doesn't point anywhere anymore */ item->prev = NULL; item->next = NULL; From cb4661cd91fc6d6f9e81b627967735f7072f901c Mon Sep 17 00:00:00 2001 From: miaoerduo Date: Fri, 3 Apr 2020 14:47:49 +0800 Subject: [PATCH 3/3] fix: errors in replacing the first item when array_size is 1, and replacing the last item --- cJSON.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cJSON.c b/cJSON.c index cc1c016..23270c4 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2313,6 +2313,10 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON } if (parent->child == item) { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } parent->child = replacement; } else @@ -2324,6 +2328,10 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON { replacement->prev->next = replacement; } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } } item->next = NULL;