From 5c18f2feeec79334e5e86a43afd0bbf30b03bf75 Mon Sep 17 00:00:00 2001 From: HiGarfield Date: Mon, 2 Sep 2024 01:54:51 +0800 Subject: [PATCH] list: use safe_free macro --- src/list.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/list.c b/src/list.c index 508b71a..856f611 100644 --- a/src/list.c +++ b/src/list.c @@ -24,6 +24,16 @@ #include "list.h" +#define safe_free(p) \ + do \ + { \ + if ((p) != NULL) \ + { \ + free((p)); \ + (p) = NULL; \ + } \ + } while (0) + #define EMPTYLIST NULL void init_list(struct list *l, int keysize) @@ -78,13 +88,12 @@ void delete_node(struct list *l, struct list_node *node) node->next->previous = node->previous; } l->count--; - free(node); + safe_free(node); } void destroy_node(struct list *l, struct list_node *node) { - free(node->data); - node->data = NULL; + safe_free(node->data); delete_node(l, node); } @@ -154,8 +163,7 @@ void clear_list(struct list *l) struct list_node *tmp; tmp = l->first; l->first = l->first->next; - free(tmp); - tmp = NULL; + safe_free(tmp); } l->last = EMPTYLIST; l->count = 0; @@ -168,10 +176,8 @@ void destroy_list(struct list *l) struct list_node *tmp; tmp = l->first; l->first = l->first->next; - free(tmp->data); - tmp->data = NULL; + safe_free(tmp->data); free(tmp); - tmp = NULL; } l->last = EMPTYLIST; l->count = 0;