From 00f09172c3f5e9d39012441ed730687f079657bd Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Fri, 8 Jun 2018 09:46:54 +0100 Subject: [PATCH] Fix cfulist_delete_data bugs 1. This function was not updating the list size 2. The first item was not being properly removed 3. Renamed some variables in order to make them more meaningful --- src/cfulist.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/cfulist.c b/src/cfulist.c index 9a5e536..661b257 100644 --- a/src/cfulist.c +++ b/src/cfulist.c @@ -414,8 +414,6 @@ cfulist_pop(cfulist_t *list) { void cfulist_delete_data(cfulist_t *list, void *data) { - cfulist_entry *ptr = NULL; - if (!list) { return; } @@ -423,16 +421,26 @@ cfulist_delete_data(cfulist_t *list, void *data) { lock_list(list); if (list->entries) { - for (ptr = list->entries; ptr && ptr->data != data; ptr = ptr->next) - ; + cfulist_entry *entry = list->entries; + while (entry && entry->data != data) + entry = entry->next; - if (ptr && ptr->data == data) { - if (!ptr->prev) { - cfulist_dequeue (list); + if (entry && entry->data == data) { + if (!entry->prev) { + if (entry->next) { + assert(list->num_entries > 1); + list->entries = entry->next; + list->entries->prev = NULL; + } else { + assert(list->num_entries == 1); + list->tail = NULL; + list->entries = NULL; + } } else { - (ptr->prev)->next = ptr->next; - free (ptr); + (entry->prev)->next = entry->next; } + free (entry); + --list->num_entries; } }