From: Lukas Lansky Date: Thu, 30 Sep 2021 07:20:05 +0000 (+0200) Subject: Addressing possible "memory leak" and "free upon failure" problems highligted by... X-Git-Tag: v23.5rc1~12^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=17099ca251ea2e3c56a44368e0ca54008239576c;p=psmisc Addressing possible "memory leak" and "free upon failure" problems highligted by cppcheck static analysis. --- diff --git a/src/killall.c b/src/killall.c index 9fab8f5..ae94c41 100644 --- a/src/killall.c +++ b/src/killall.c @@ -510,7 +510,7 @@ load_proc_cmdline(const pid_t pid, const char *comm, char **command, int *got_lo static pid_t * create_pid_table(int *max_pids, int *pids) { - pid_t self, *pid_table; + pid_t self, *pid_table, *realloc_pid_table; int pid; DIR *dir; struct dirent *de; @@ -535,11 +535,13 @@ create_pid_table(int *max_pids, int *pids) continue; if (*pids == *max_pids) { - if (!(pid_table = realloc (pid_table, 2 * *pids * sizeof (pid_t)))) + if (!(realloc_pid_table = realloc (pid_table, 2 * *pids * sizeof (pid_t)))) { perror ("realloc"); + free(pid_table); exit (1); } + pid_table = realloc_pid_table; *max_pids *= 2; } pid_table[(*pids)++] = pid; diff --git a/src/pslog.c b/src/pslog.c index d60723f..cd0c3e2 100644 --- a/src/pslog.c +++ b/src/pslog.c @@ -122,11 +122,13 @@ main(int argc, char const *argv[]) if (argv[1][0] != '/') { if (asprintf(&fullpath, "/proc/%s/fd/", argv[1]) < 0) { perror ("asprintf"); + free(linkpath); return 1; } } else { if (asprintf(&fullpath, "%s/fd/", argv[1]) < 0) { perror("asprintf"); + free(linkpath); return 1; } } @@ -134,6 +136,7 @@ main(int argc, char const *argv[]) pid_dir = opendir(fullpath); if (!pid_dir) { perror("opendir"); + free(linkpath); free(fullpath); return 1; }