Buffer and memory allocation related bug fixes and changes
authorCosmin Ioiart <cioiart+dev@gmail.com>
Wed, 7 Mar 2012 11:33:12 +0000 (12:33 +0100)
committerFlorian Forster <octo@collectd.org>
Sun, 23 Sep 2012 10:39:31 +0000 (12:39 +0200)
Replaced "free" with "sfree" for memory deallocation.
Added memset(...) to initialized newly allocated memory
Removed buffer "termination".

src/processes.c

index 61955f8..2084251 100644 (file)
@@ -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);
 }