]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc_monitor: fix memory leak on @fds and close fds
authorArjun Sreedharan <arjun024@gmail.com>
Wed, 10 Jun 2015 18:24:15 +0000 (23:54 +0530)
committerSerge Hallyn <serge.hallyn@ubuntu.com>
Mon, 29 Jun 2015 14:58:43 +0000 (09:58 -0500)
also label and consolidate error conditions for
better readability

Signed-off-by: Arjun Sreedharan <arjun024@gmail.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxc_monitor.c

index b3d8912c2729e807e0738c23f46c99392cc6146e..3f967028f17280817fefdba7814f7ed86387889b 100644 (file)
@@ -71,6 +71,18 @@ Options :\n\
        .lxcpath_additional = -1,
 };
 
+static void close_fds(struct pollfd *fds, nfds_t nfds)
+{
+       nfds_t i;
+
+       if (nfds < 1)
+               return;
+
+       for (i = 0; i < nfds; ++i) {
+               close(fds[i].fd);
+       }
+}
+
 int main(int argc, char *argv[])
 {
        char *regexp;
@@ -78,7 +90,9 @@ int main(int argc, char *argv[])
        regex_t preg;
        struct pollfd *fds;
        nfds_t nfds;
-       int len, rc, i;
+       int len, rc_main, rc_snp, i;
+
+       rc_main = 0;
 
        if (lxc_arguments_parse(&my_args, argc, argv))
                return 1;
@@ -119,24 +133,24 @@ int main(int argc, char *argv[])
                ERROR("failed to allocate memory");
                return 1;
        }
-       rc = snprintf(regexp, len, "^%s$", my_args.name);
-       if (rc < 0 || rc >= len) {
+       rc_snp = snprintf(regexp, len, "^%s$", my_args.name);
+       if (rc_snp < 0 || rc_snp >= len) {
                ERROR("Name too long");
-               free(regexp);
-               return 1;
+               rc_main = 1;
+               goto error;
        }
 
        if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) {
                ERROR("failed to compile the regex '%s'", my_args.name);
-               free(regexp);
-               return 1;
+               rc_main = 1;
+               goto error;
        }
-       free(regexp);
 
        fds = malloc(my_args.lxcpath_cnt * sizeof(struct pollfd));
        if (!fds) {
                SYSERROR("out of memory");
-               return -1;
+               rc_main = -1;
+               goto error;
        }
 
        nfds = my_args.lxcpath_cnt;
@@ -147,8 +161,9 @@ int main(int argc, char *argv[])
 
                fd = lxc_monitor_open(my_args.lxcpath[i]);
                if (fd < 0) {
-                       regfree(&preg);
-                       return 1;
+                       close_fds(fds, i);
+                       rc_main = 1;
+                       goto cleanup;
                }
                fds[i].fd = fd;
                fds[i].events = POLLIN;
@@ -159,8 +174,8 @@ int main(int argc, char *argv[])
 
        for (;;) {
                if (lxc_monitor_read_fdset(fds, nfds, &msg, -1) < 0) {
-                       regfree(&preg);
-                       return 1;
+                       rc_main = 1;
+                       goto close_and_clean;
                }
 
                msg.name[sizeof(msg.name)-1] = '\0';
@@ -182,7 +197,15 @@ int main(int argc, char *argv[])
                }
        }
 
+close_and_clean:
+       close_fds(fds, nfds);
+
+cleanup:
        regfree(&preg);
+       free(fds);
 
-       return 0;
+error:
+       free(regexp);
+
+       return rc_main;
 }