]> granicus.if.org Git - psmisc/commitdiff
misc: Fix some memory leaks from coverity
authorCraig Small <csmall@enc.com.au>
Mon, 25 Apr 2016 12:35:47 +0000 (22:35 +1000)
committerCraig Small <csmall@enc.com.au>
Mon, 25 Apr 2016 12:35:47 +0000 (22:35 +1000)
Ran psmisc through coverity scan and it found a few places
where programs were not checking return values or freeing
malloc'ed items.

src/fuser.c
src/killall.c
src/prtstat.c

index 33bc10fcf3a0e6f4b33e35b93775ff719eb8ceb0..58b5ccbd7de9de74ad4179cc859bb675c62f7ce5 100644 (file)
@@ -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;
                }
        }
index 3b1e0dca12bc981389c5ccd9ca395641cf1e02ff..9f9a74d11b4f2c47a904a3ff60274d502dba98cd 100644 (file)
@@ -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");
 }
index 2f8679983c6713f15ad85e76d671f0a4b7dbc986..441a9a096adecc0247344ea8a92b645b59cac3ba 100644 (file)
@@ -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);
 }