]> granicus.if.org Git - psmisc/commitdiff
Addressing possible "memory leak" and "free upon failure" problems highligted by...
authorLukas Lansky <lukas.lansky@suse.com>
Thu, 30 Sep 2021 07:20:05 +0000 (09:20 +0200)
committerLukas Lansky <lukas.lansky@suse.com>
Thu, 30 Sep 2021 07:20:05 +0000 (09:20 +0200)
src/killall.c
src/pslog.c

index 9fab8f524ec25011f1e08a047116e52203c73530..ae94c419f3feecf5442ef4d9a430e91245a43388 100644 (file)
@@ -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;
index d60723fe51a30b9be0d8bc5301fa85a947a6aced..cd0c3e25a7c1f3a347fe2ab3f6f40a408954b1f9 100644 (file)
@@ -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;
   }