From b9e683b7e6c61f5ef8185482cf15e7cdd7cb4e58 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 11 Mar 2012 15:53:39 +0100 Subject: [PATCH] ethstat module: Fix allocation of "ifacelist". And remove uses of strcpy(3). --- src/ethstat.c | 29 ++++++++++++++++++++--------- src/ethstat.h | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/ethstat.c b/src/ethstat.c index d3343746..8bc47ad4 100644 --- a/src/ethstat.c +++ b/src/ethstat.c @@ -32,15 +32,29 @@ static int ethstat_config (const char *key, const char *value) { - if (strcasecmp (key, "Iface") == 0) { - ifacelist[ifacenumber] = malloc(strlen(value) + 1); - strcpy(ifacelist[ifacenumber++], value); - INFO("ethstat: Registred iface %s", value); + if (strcasecmp ("Iface", key) == 0) + { + char **tmp; + + tmp = realloc (ifacelist, + sizeof (*ifacelist) * (ifacenumber + 1)); + if (tmp == NULL) + return (-1); + ifacelist = tmp; + + ifacelist[ifacenumber] = strdup (value); + if (ifacelist[ifacenumber] == NULL) + { + ERROR ("ethstat plugin: strdup() failed."); + return (-1); + } + + ifacenumber++; + INFO("ethstat plugin: Registred interface %s", value); } return (0); } - static void ethstat_submit_value (char *devname, char *counter, unsigned long long value) { value_t values[1]; @@ -136,11 +150,9 @@ static int getstats(char *devname, struct ifreq *ifr) { free(strings); free(stats); - return 0; } - static int ethstat_read(void) { struct ifreq ifr; @@ -149,7 +161,7 @@ static int ethstat_read(void) for (i = 0 ; i < ifacenumber ; i++) { DEBUG("ethstat - Processing : %s\n", ifacelist[i]); memset(&ifr, 0, sizeof(ifr)); - strcpy(ifr.ifr_name, ifacelist[i]); + sstrncpy(ifr.ifr_name, ifacelist[i], sizeof (ifr.ifr_name)); getstats(ifacelist[i], &ifr); } return 0; @@ -157,7 +169,6 @@ static int ethstat_read(void) void module_register (void) { - ifacelist = malloc(sizeof(char*)); plugin_register_config ("ethstat", ethstat_config, config_keys, config_keys_num); plugin_register_read ("ethstat", ethstat_read); diff --git a/src/ethstat.h b/src/ethstat.h index d544db51..531e2be0 100644 --- a/src/ethstat.h +++ b/src/ethstat.h @@ -44,7 +44,7 @@ static const char *config_keys[] = }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); -static char **ifacelist; +static char **ifacelist = NULL; static int ifacenumber = 0; struct ethtool_drvinfo { __u32 cmd; -- 2.11.0