From: Craig Small Date: Mon, 25 Apr 2016 12:35:47 +0000 (+1000) Subject: misc: Fix some memory leaks from coverity X-Git-Tag: v23.0rc1~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=013c6a569837a04b2f1df330cb647eec9db13fbc;p=psmisc misc: Fix some memory leaks from coverity Ran psmisc through coverity scan and it found a few places where programs were not checking return values or freeing malloc'ed items. --- diff --git a/src/fuser.c b/src/fuser.c index 33bc10f..58b5ccb 100644 --- a/src/fuser.c +++ b/src/fuser.c @@ -608,6 +608,7 @@ int parse_inet(struct names *this_name, struct ip_connections **ip_list) fprintf(stderr, _("Unknown local port AF %d\n"), res->ai_family); freeaddrinfo(res); + free(lcl_port_str); return -1; } freeaddrinfo(res); @@ -671,6 +672,7 @@ int parse_inet(struct names *this_name, struct ip_connections **ip_list) #endif } } /*while */ + freeaddrinfo(res); return 0; } } diff --git a/src/killall.c b/src/killall.c index 3b1e0dc..9f9a74d 100644 --- a/src/killall.c +++ b/src/killall.c @@ -369,6 +369,8 @@ load_proc_cmdline(const pid_t pid, const char *comm, char **command, int *got_lo } } (void) fclose(file); + free(command_buf); + command_buf = NULL; if (exact && !okay) { @@ -650,6 +652,7 @@ kill_all (int signal, int name_count, char **namelist, struct passwd *pwent) } free(pid_killed); free(pid_table); + free(command); return error; } @@ -894,5 +897,4 @@ main (int argc, char **argv) #else /*WITH_SELINUX*/ return kill_all(sig_num,argc - myoptind, argv, pwent); #endif /*WITH_SELINUX*/ - printf("done\n"); } diff --git a/src/prtstat.c b/src/prtstat.c index 2f86799..441a9a0 100644 --- a/src/prtstat.c +++ b/src/prtstat.c @@ -239,8 +239,11 @@ static void print_stat(const int pid, const opt_type options) bptr = strchr(buf, '('); if (bptr == NULL) return; bptr++; - pr = malloc(sizeof(struct proc_info)); - sscanf(bptr, + if ((pr = malloc(sizeof(struct proc_info))) == NULL) { + fprintf(stderr, _("Unable to allocate memory for proc_info\n")); + return; + } + if (sscanf(bptr, "%m[^)]) " "%c " "%d %d %d %d %d %d" @@ -270,11 +273,13 @@ static void print_stat(const int pid, const opt_type options) &pr->exit_signal, &pr->processor, &pr->rt_priority, &pr->policy, &pr->blkio, &pr->guest_time, &pr->cguest_time - ); - if (options & OPT_RAW) - print_raw_stat(pid, pr); - else - print_formated_stat(pid, pr); + ) == 39) { + if (options & OPT_RAW) + print_raw_stat(pid, pr); + else + print_formated_stat(pid, pr); + } else + fprintf(stderr, _("Unable to scan stat file")); free(pr); }