From c1648e6558e10d26e4d2fa091902a535c08d8254 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 12:04:28 +0100 Subject: [PATCH] Use free_and_zero in [u-z]*.cc where appropriate --- src/tailhead.cc | 1 + src/text_object.cc | 1 + src/users.cc | 63 ++++++++++------------------------------------ src/weather.cc | 5 +--- src/x11.cc | 20 +++------------ src/xmms2.cc | 19 +++++++------- 6 files changed, 29 insertions(+), 80 deletions(-) diff --git a/src/tailhead.cc b/src/tailhead.cc index fc059b89..be135315 100644 --- a/src/tailhead.cc +++ b/src/tailhead.cc @@ -28,6 +28,7 @@ * */ +#include "conky.h" #include "config.h" #include "common.h" #include "text_object.h" diff --git a/src/text_object.cc b/src/text_object.cc index 7734782e..efb6dd4d 100644 --- a/src/text_object.cc +++ b/src/text_object.cc @@ -26,6 +26,7 @@ * along with this program. If not, see . * */ +#include "conky.h" #include "config.h" #include "text_object.h" #include "logging.h" diff --git a/src/users.cc b/src/users.cc index 9e3df172..ac36dab8 100644 --- a/src/users.cc +++ b/src/users.cc @@ -141,17 +141,11 @@ static void update_user_time(char *tty) tty_user_time(temp, tty); if (temp != NULL) { - if (current_info->users.ctime) { - free(current_info->users.ctime); - current_info->users.ctime = 0; - } + free_and_zero(current_info->users.ctime); current_info->users.ctime = (char*)malloc(text_buffer_size); strncpy(current_info->users.ctime, temp, text_buffer_size); } else { - if (current_info->users.ctime) { - free(current_info->users.ctime); - current_info->users.ctime = 0; - } + free_and_zero(current_info->users.ctime); current_info->users.ctime = (char*)malloc(text_buffer_size); strncpy(current_info->users.ctime, "broken", text_buffer_size); } @@ -165,17 +159,11 @@ void update_users(void) users_alloc(current_info); user_name(temp); if (temp != NULL) { - if (current_info->users.names) { - free(current_info->users.names); - current_info->users.names = 0; - } + free_and_zero(current_info->users.names); current_info->users.names = (char*)malloc(text_buffer_size); strncpy(current_info->users.names, temp, text_buffer_size); } else { - if (current_info->users.names) { - free(current_info->users.names); - current_info->users.names = 0; - } + free_and_zero(current_info->users.names); current_info->users.names = (char*)malloc(text_buffer_size); strncpy(current_info->users.names, "broken", text_buffer_size); } @@ -191,33 +179,21 @@ void update_users(void) temp[0] = 0; user_term(temp); if (temp != NULL) { - if (current_info->users.terms) { - free(current_info->users.terms); - current_info->users.terms = 0; - } + free_and_zero(current_info->users.terms); current_info->users.terms = (char*)malloc(text_buffer_size); strncpy(current_info->users.terms, temp, text_buffer_size); } else { - if (current_info->users.terms) { - free(current_info->users.terms); - current_info->users.terms = 0; - } + free_and_zero(current_info->users.terms); current_info->users.terms = (char*)malloc(text_buffer_size); strncpy(current_info->users.terms, "broken", text_buffer_size); } user_time(temp); if (temp != NULL) { - if (current_info->users.times) { - free(current_info->users.times); - current_info->users.times = 0; - } + free_and_zero(current_info->users.times); current_info->users.times = (char*)malloc(text_buffer_size); strncpy(current_info->users.times, temp, text_buffer_size); } else { - if (current_info->users.times) { - free(current_info->users.times); - current_info->users.times = 0; - } + free_and_zero(current_info->users.times); current_info->users.times = (char*)malloc(text_buffer_size); strncpy(current_info->users.times, "broken", text_buffer_size); } @@ -256,36 +232,23 @@ void print_user_number(struct text_object *obj, char *p, int p_max_size) void free_user_names(struct text_object *obj) { (void)obj; - if (info.users.names) { - free(info.users.names); - info.users.names = 0; - } + free_and_zero(info.users.names); } void free_user_terms(struct text_object *obj) { (void)obj; - if (info.users.terms) { - free(info.users.terms); - info.users.terms = 0; - } + free_and_zero(info.users.terms); } void free_user_times(struct text_object *obj) { (void)obj; - if (info.users.times) { - free(info.users.times); - info.users.times = 0; - } + free_and_zero(info.users.times); } void free_user_time(struct text_object *obj) { - if (info.users.ctime) { - free(info.users.ctime); - info.users.ctime = 0; - } - if (obj->data.s) - free(obj->data.s); + free_and_zero(info.users.ctime); + free_and_zero(obj->data.s); } diff --git a/src/weather.cc b/src/weather.cc index f2c2e4cd..32bf4e48 100644 --- a/src/weather.cc +++ b/src/weather.cc @@ -1010,8 +1010,5 @@ void print_weather(struct text_object *obj, char *p, int p_max_size) void free_weather(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } diff --git a/src/x11.cc b/src/x11.cc index 12e9c454..8285b8b8 100644 --- a/src/x11.cc +++ b/src/x11.cc @@ -698,10 +698,7 @@ static inline void get_x11_desktop_names(Display *current_display, Window root, (actual_type == ATOM(UTF8_STRING)) && (nitems > 0L) && (actual_format == 8) ) { - if(current_info->x11.desktop.all_names) { - free(current_info->x11.desktop.all_names); - current_info->x11.desktop.all_names = NULL; - } + free_and_zero(current_info->x11.desktop.all_names); current_info->x11.desktop.all_names = (char*)malloc(nitems*sizeof(char)); memcpy(current_info->x11.desktop.all_names, prop, nitems); current_info->x11.desktop.nitems = nitems; @@ -721,10 +718,7 @@ static inline void get_x11_desktop_current_name(char *names) while ( i < current_info->x11.desktop.nitems ) { if ( names[i++] == '\0' ) { if ( ++k == current_info->x11.desktop.current ) { - if (current_info->x11.desktop.name) { - free(current_info->x11.desktop.name); - current_info->x11.desktop.name = NULL; - } + free_and_zero(current_info->x11.desktop.name); current_info->x11.desktop.name = (char*)malloc((i-j)*sizeof(char)); //desktop names can be empty but should always be not null strcpy( current_info->x11.desktop.name, (char *)&names[j] ); @@ -834,14 +828,8 @@ void print_desktop_name(struct text_object *obj, char *p, int p_max_size) void free_desktop_info(void) { - if(info.x11.desktop.name) { - free(info.x11.desktop.name); - info.x11.desktop.name = NULL; - } - if(info.x11.desktop.all_names) { - free(info.x11.desktop.all_names); - info.x11.desktop.all_names = NULL; - } + free_and_zero(info.x11.desktop.name); + free_and_zero(info.x11.desktop.all_names); } #ifdef OWN_WINDOW diff --git a/src/xmms2.cc b/src/xmms2.cc index 97ac9a01..86a2d194 100644 --- a/src/xmms2.cc +++ b/src/xmms2.cc @@ -84,20 +84,19 @@ static void xmms_alloc(struct information *ptr) ptr->xmms2.timesplayed = -1; } -#define xfree(x) if (x) { free(x); x = 0; } void free_xmms2(struct text_object *obj) { (void)obj; - xfree(info.xmms2.artist); - xfree(info.xmms2.album); - xfree(info.xmms2.title); - xfree(info.xmms2.genre); - xfree(info.xmms2.comment); - xfree(info.xmms2.url); - xfree(info.xmms2.date); - xfree(info.xmms2.status); - xfree(info.xmms2.playlist); + free_and_zero(info.xmms2.artist); + free_and_zero(info.xmms2.album); + free_and_zero(info.xmms2.title); + free_and_zero(info.xmms2.genre); + free_and_zero(info.xmms2.comment); + free_and_zero(info.xmms2.url); + free_and_zero(info.xmms2.date); + free_and_zero(info.xmms2.status); + free_and_zero(info.xmms2.playlist); } void connection_lost(void *p)