From 7303bc8f00c93ea980dbda93b9ebababf29d32e8 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Wed, 21 May 2014 12:50:56 +0300 Subject: [PATCH] More guarding against freeing NULL pointers. --- src/list.c | 25 +++++++++++++------------ src/token.c | 12 ++++++++---- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/list.c b/src/list.c index 62b7729..bf27bbc 100644 --- a/src/list.c +++ b/src/list.c @@ -25,22 +25,23 @@ void list_free(l) list *l; { - list_item *li, *tmp; + if (l) { + list_item *li, *tmp; - pthread_mutex_lock(&(l->mutex)); + pthread_mutex_lock(&(l->mutex)); - if (l != NULL) { - li = l->head; - while (li != NULL) { - tmp = li->next; - zfree(li); - li = tmp; + if (l != NULL) { + li = l->head; + while (li != NULL) { + tmp = li->next; + li = tmp; + } } - } - pthread_mutex_unlock(&(l->mutex)); - pthread_mutex_destroy(&(l->mutex)); - zfree(l); + pthread_mutex_unlock(&(l->mutex)); + pthread_mutex_destroy(&(l->mutex)); + zfree(l); + } } list_item * diff --git a/src/token.c b/src/token.c index e0f23a8..f5f73eb 100644 --- a/src/token.c +++ b/src/token.c @@ -21,11 +21,15 @@ str_array * str_array_create(int cap) { } void str_array_free(str_array *l) { - for ( int i = 0; i < l->len ; i++ ) { - char * t = l->tokens[ i ]; - zfree(t); + if (l) { + for ( int i = 0; i < l->len ; i++ ) { + char * t = l->tokens[ i ]; + if (t) { + zfree(t); + } + } + zfree(l); } - zfree(l); } bool str_array_is_full(str_array * l) {