From: Cosmin Ioiart Date: Wed, 7 Mar 2012 11:33:12 +0000 (+0100) Subject: Buffer and memory allocation related bug fixes and changes X-Git-Tag: collectd-5.2.0~58 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=cb6fbadd9ff71988de4eaa0218007ef0916c7755;p=collectd.git Buffer and memory allocation related bug fixes and changes Replaced "free" with "sfree" for memory deallocation. Added memset(...) to initialized newly allocated memory Removed buffer "termination". --- diff --git a/src/processes.c b/src/processes.c index 61955f82..20842518 100644 --- a/src/processes.c +++ b/src/processes.c @@ -1198,20 +1198,21 @@ static void ps_submit_fork_rate (unsigned long value) static char *ps_get_cmdline(pid_t pid) { char f_psinfo[64]; + char cmdline[80]; char *buffer = NULL; psinfo_t *myInfo; snprintf(f_psinfo, sizeof (f_psinfo), "/proc/%i/psinfo", pid); - buffer = malloc(sizeof (psinfo_t)); + buffer = (char *)malloc(sizeof (psinfo_t)); + memset(buffer, 0, sizeof(psinfo_t)); read_file_contents(f_psinfo, buffer, sizeof (psinfo_t)); - buffer[sizeof (psinfo_t)] = 0; myInfo = (psinfo_t *) buffer; - sstrncpy(buffer, myInfo->pr_psargs, sizeof (myInfo->pr_psargs)); + sstrncpy(cmdline, myInfo->pr_psargs, sizeof (myInfo->pr_psargs)); - free(myInfo); - return strtok(buffer, " "); + sfree(myInfo); + return strtok(cmdline, " "); } /* @@ -1239,15 +1240,17 @@ static int ps_read_process(int pid, procstat_t *ps, char *state) buffer = malloc(sizeof (pstatus_t)); + memset(buffer, 0, sizeof (pstatus_t)); read_file_contents(filename, buffer, sizeof (pstatus_t)); myStatus = (pstatus_t *) buffer; buffer = malloc(sizeof (psinfo_t)); + memset(buffer, 0, sizeof(psinfo_t)); read_file_contents(f_psinfo, buffer, sizeof (psinfo_t)); - buffer[sizeof (psinfo_t)] = 0; myInfo = (psinfo_t *) buffer; buffer = malloc(sizeof (prusage_t)); + memset(buffer, 0, sizeof(prusage_t)); read_file_contents(f_usage, buffer, sizeof (prusage_t)); myUsage = (prusage_t *) buffer; @@ -1320,9 +1323,9 @@ static int ps_read_process(int pid, procstat_t *ps, char *state) else if (myStatus->pr_flags & PR_ORPHAN) *state = (char) 'O'; - free(myStatus); - free(myInfo); - free(myUsage); + sfree(myStatus); + sfree(myInfo); + sfree(myUsage); return (0); }