From 1fcc2b17ab8e88a023d3cd38057e9a5fe93d7313 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Mon, 19 May 2014 22:31:34 +1000 Subject: Minor fixes due to Coverity reporting --- .gitignore | 1 + src/fuser.c | 15 +++++++++++++++ src/killall.c | 22 +++++++++++++--------- src/prtstat.c | 23 +++++++++++++---------- src/pstree.c | 2 +- 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 958ba9f..ed840f2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ config.h.in config.log config.status configure +cov-int/ doc/Makefile doc/Makefile.in icons/Makefile diff --git a/src/fuser.c b/src/fuser.c index 389b302..8d9bd52 100644 --- a/src/fuser.c +++ b/src/fuser.c @@ -866,6 +866,20 @@ static void read_proc_mounts(struct mount_list **mnt_list) fclose(fp); } +static void +free_proc_mounts(struct mount_list *mnt_list) +{ + struct mount_list *mnt_tmp, *mnt_next; + + mnt_tmp = mnt_list; + while(mnt_tmp != NULL) { + mnt_next = mnt_tmp->next; + free(mnt_tmp->mountpoint); + free(mnt_tmp); + mnt_tmp = mnt_next; + } +} + static int is_mountpoint(struct mount_list **mnt_list, char *arg) { char *p; @@ -1216,6 +1230,7 @@ int main(int argc, char *argv[]) scan_knfsd(names_head, match_inodes, match_devices); scan_mounts(names_head, match_inodes, match_devices); scan_swaps(names_head, match_inodes, match_devices); + free_proc_mounts(mounts); return print_matches(names_head, opts, sig_number); } diff --git a/src/killall.c b/src/killall.c index 9f2782a..b1a6d18 100644 --- a/src/killall.c +++ b/src/killall.c @@ -259,7 +259,7 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) char *command_buf; char *command; pid_t *pid_table, pid, self, *pid_killed; - pid_t *pgids; + pid_t *pgids = NULL; int i, j, okay, length, got_long, error; int pids, max_pids, pids_killed; unsigned long found; @@ -330,16 +330,14 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) exit (1); } if (!process_group) - pgids = NULL; /* silence gcc */ - else - { + { pgids = calloc (pids, sizeof (pid_t)); if (!pgids) { perror ("malloc"); exit (1); } - } + } for (i = 0; i < pids; i++) { pid_t id; @@ -400,13 +398,13 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) continue; } free (path); + int cmd_size = 128; + command_buf = (char *)malloc (cmd_size); + if (!command_buf) + exit (1); while (1) { /* look for actual command so we skip over initial "sh" if any */ char *p; - int cmd_size = 128; - command_buf = (char *)malloc (cmd_size); - if (!command_buf) - exit (1); /* 'cmdline' has arguments separated by nulls */ for (p=command_buf; ; p++) { @@ -440,6 +438,7 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) break; } } + free(command_buf); (void) fclose(file); if (exact && !okay) { @@ -530,6 +529,8 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) found_name = j; break; } + free(reglist); + free(name_len); if (names && found_name==-1) continue; /* match by process name faild */ @@ -570,6 +571,7 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) fprintf (stderr, "%s(%d): %s\n", got_long ? command : comm, id, strerror (errno)); } + free(pgids); if (!quiet) for (i = 0; i < names; i++) if (!(found & (1 << i))) @@ -601,6 +603,8 @@ kill_all (int signal, int names, char **namelist, struct passwd *pwent) } sleep (1); /* wait a bit longer */ } + free(pid_killed); + free(pid_table); return error; } diff --git a/src/prtstat.c b/src/prtstat.c index 7ed2bee..0a6ace4 100644 --- a/src/prtstat.c +++ b/src/prtstat.c @@ -1,7 +1,7 @@ /* * prtstat.c - Print a processes stat file * - * Copyright (C) 2009 Craig Small + * Copyright (C) 2009-2014 Craig Small * Based upon a shell script pstat by martin f. krafft * * This program is free software; you can redistribute it and/or modify @@ -215,7 +215,6 @@ static void print_stat(const int pid, const opt_type options) FILE *fp; struct proc_info *pr; - pr = malloc(sizeof(struct proc_info)); if ( (asprintf(&pathname, "/proc/%d/stat",(int)pid)) < 0) { perror(_("asprintf in print_stat failed.\n")); @@ -227,14 +226,20 @@ static void print_stat(const int pid, const opt_type options) else fprintf(stderr, _("Unable to open stat file for pid %d (%s)\n"),(int)pid,strerror(errno)); free(pathname); + free(pr); return; } free(pathname); - if (fgets(buf,BUFSIZ,fp) == NULL) return; + if (fgets(buf,BUFSIZ,fp) == NULL) { + fclose(fp); + return; + } + fclose(fp); bptr = strchr(buf, '('); if (bptr == NULL) return; bptr++; + pr = malloc(sizeof(struct proc_info)); sscanf(bptr, "%a[^)]) " "%c " @@ -266,13 +271,11 @@ static void print_stat(const int pid, const opt_type options) &pr->policy, &pr->blkio, &pr->guest_time, &pr->cguest_time ); - if (options & OPT_RAW) { - print_raw_stat(pid, pr); - return; - } - print_formated_stat(pid, pr); - - + if (options & OPT_RAW) + print_raw_stat(pid, pr); + else + print_formated_stat(pid, pr); + free(pr); } int main(int argc, char *argv[]) diff --git a/src/pstree.c b/src/pstree.c index d13630f..6d6e8db 100644 --- a/src/pstree.c +++ b/src/pstree.c @@ -813,7 +813,7 @@ static char* get_threadname(const pid_t pid, const int tid, const char *comm) if (snprintf(path, PATH_MAX, "%s/%d/task/%d/stat", PROC_BASE, pid, tid) < 0) perror("get_threadname: asprintf"); if ( (file = fopen(path, "r")) != NULL) { - if (fread(readbuf, 1, BUFSIZ, file) > 0) { + if (fgets(readbuf, BUFSIZ, file) != NULL) { if ((thread_comm = strchr(readbuf, '(')) && (endcomm = strrchr(thread_comm, ')'))) { ++thread_comm; -- cgit v1.2.3