diff --git a/libifupdown/dict.c b/libifupdown/dict.c index a0b5f2a..3dea682 100644 --- a/libifupdown/dict.c +++ b/libifupdown/dict.c @@ -14,6 +14,7 @@ * from the use of this software. */ +#include #include #include #include "libifupdown/dict.h" @@ -55,14 +56,22 @@ lif_dict_add_once(struct lif_dict *dict, const char *key, void *data, int (*compar)(const void *, const void *)) { struct lif_list *existing = lif_dict_find_all(dict, key); - if (existing) + if (existing != NULL) { + bool found = false; struct lif_node *iter; LIF_LIST_FOREACH(iter, existing->head) { if (!compar(data, iter->data)) - return NULL; + { + found = true; + break; + } } + + lif_list_free_nodes (&existing); + if (found) + return NULL; } struct lif_dict_entry *entry = calloc(1, sizeof *entry); diff --git a/libifupdown/list.c b/libifupdown/list.c index 6a44f86..6b92b1c 100644 --- a/libifupdown/list.c +++ b/libifupdown/list.c @@ -3,6 +3,7 @@ * Purpose: linked lists * * Copyright (c) 2020 Ariadne Conill + * Copyright (c) 2020 Maximilian Wilhelm * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,8 +15,26 @@ */ #include +#include #include "libifupdown/list.h" +void +lif_list_free_nodes(struct lif_list **list) +{ + if (*list == NULL) + return; + + struct lif_node *iter; + + LIF_LIST_FOREACH (iter, (*list)->head) + { + free (iter->prev); + } + + free (iter); + free (*list); +} + void lif_node_insert(struct lif_node *node, void *data, struct lif_list *list) { diff --git a/libifupdown/list.h b/libifupdown/list.h index 326eca0..a0155d6 100644 --- a/libifupdown/list.h +++ b/libifupdown/list.h @@ -3,6 +3,7 @@ * Purpose: linked lists * * Copyright (c) 2020 Ariadne Conill + * Copyright (c) 2020 Maximilian Wilhelm * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -29,6 +30,8 @@ struct lif_list { size_t length; }; +extern void lif_list_free_nodes(struct lif_list **list); + extern void lif_node_insert(struct lif_node *node, void *data, struct lif_list *list); extern void lif_node_insert_tail(struct lif_node *node, void *data, struct lif_list *list); extern void lif_node_delete(struct lif_node *node, struct lif_list *list);