From 293f6ad7e20a82ebd03c52fd2319637b8b1ccd8a Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Mon, 16 Mar 2009 15:56:37 -0600 Subject: [PATCH] Fix possible mem leak with net stats. --- src/common.h | 4 +++- src/linux.c | 15 +++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/common.h b/src/common.h index 9f3977cb..9658453f 100644 --- a/src/common.h +++ b/src/common.h @@ -55,7 +55,9 @@ struct net_stat { long long recv, trans; double recv_speed, trans_speed; struct sockaddr addr; - char* addrs; +#if defined(__linux__) + char addrs[273]; +#endif /* __linux__ */ double net_rec[15], net_trans[15]; // wireless extensions char essid[32]; diff --git a/src/linux.c b/src/linux.c index 555d3419..1afcd601 100644 --- a/src/linux.c +++ b/src/linux.c @@ -381,10 +381,7 @@ void update_net_stats(void) ns->up = 1; memset(&(ns->addr.sa_data), 0, 14); - if(NULL == ns->addrs) - ns->addrs = (char*) malloc(17 * 16 + 1); - if(NULL != ns->addrs) - memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */ + memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */ last_recv = ns->recv; last_trans = ns->trans; @@ -424,17 +421,15 @@ void update_net_stats(void) break; ns2 = get_net_stat( - ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name); + ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name); ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr; - if(NULL != ns2->addrs) { - sprintf(temp_addr, "%u.%u.%u.%u, ", + sprintf(temp_addr, "%u.%u.%u.%u, ", ns2->addr.sa_data[2] & 255, ns2->addr.sa_data[3] & 255, ns2->addr.sa_data[4] & 255, ns2->addr.sa_data[5] & 255); - if(NULL == strstr(ns2->addrs, temp_addr)) - strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17); - } + if(NULL == strstr(ns2->addrs, temp_addr)) + strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17); } close((long) i);