]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Fixed memory and file descriptor leak when enumerating processes
authorJan Safranek <jsafrane@redhat.com>
Wed, 24 Aug 2011 09:40:50 +0000 (11:40 +0200)
committerJan Safranek <jsafrane@redhat.com>
Wed, 14 Sep 2011 11:23:04 +0000 (13:23 +0200)
Free the resources when fopen or realloc fails.
Also remove completelly useless code.

Signed-off-by: Jan Safranek <jsafrane@redhat.com>
Acked-by: Dhaval Giani <dhaval.giani@gmail.com>
src/api.c

index 5b2b518d196ca2c71c9d0e14d77b6d35b0f21ea0..2ed9e2233f11d246ade7cd0a63057aa304c9dc16 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -3882,12 +3882,6 @@ int cgroup_get_procs(char *name, char *controller, pid_t **pids, int *size)
        if (access(cgroup_path, F_OK))
                return ECGROUPUNSUPP;
 
-       /*
-        * Read all the procs and then sort them up.
-        */
-
-       tmp_list = *pids;
-
        /*
         * Keep doubling the memory allocated if needed
         */
@@ -3900,6 +3894,9 @@ int cgroup_get_procs(char *name, char *controller, pid_t **pids, int *size)
        procs = fopen(cgroup_path, "r");
        if (!procs) {
                last_errno = errno;
+               free(tmp_list);
+               *pids = NULL;
+               *size = 0;
                return ECGOTHER;
        }
 
@@ -3913,10 +3910,15 @@ int cgroup_get_procs(char *name, char *controller, pid_t **pids, int *size)
                        n++;
                }
                if (!feof(procs)) {
+                       pid_t *orig_list = tmp_list;
                        tot_procs *= 2;
                        tmp_list = realloc(tmp_list, sizeof(pid_t) * tot_procs);
                        if (!tmp_list) {
                                last_errno = errno;
+                               fclose(procs);
+                               free(orig_list);
+                               *pids = NULL;
+                               *size = 0;
                                return ECGOTHER;
                        }
                }