Commit 57354986 by Arjun Sreedharan Committed by Serge Hallyn

lxc_monitor: fix memory leak on @fds and close fds

also label and consolidate error conditions for better readability Signed-off-by: 's avatarArjun Sreedharan <arjun024@gmail.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent d791668b
...@@ -71,6 +71,18 @@ Options :\n\ ...@@ -71,6 +71,18 @@ Options :\n\
.lxcpath_additional = -1, .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[]) int main(int argc, char *argv[])
{ {
char *regexp; char *regexp;
...@@ -78,7 +90,9 @@ int main(int argc, char *argv[]) ...@@ -78,7 +90,9 @@ int main(int argc, char *argv[])
regex_t preg; regex_t preg;
struct pollfd *fds; struct pollfd *fds;
nfds_t nfds; 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)) if (lxc_arguments_parse(&my_args, argc, argv))
return 1; return 1;
...@@ -119,24 +133,24 @@ int main(int argc, char *argv[]) ...@@ -119,24 +133,24 @@ int main(int argc, char *argv[])
ERROR("failed to allocate memory"); ERROR("failed to allocate memory");
return 1; return 1;
} }
rc = snprintf(regexp, len, "^%s$", my_args.name); rc_snp = snprintf(regexp, len, "^%s$", my_args.name);
if (rc < 0 || rc >= len) { if (rc_snp < 0 || rc_snp >= len) {
ERROR("Name too long"); ERROR("Name too long");
free(regexp); rc_main = 1;
return 1; goto error;
} }
if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) { if (regcomp(&preg, regexp, REG_NOSUB|REG_EXTENDED)) {
ERROR("failed to compile the regex '%s'", my_args.name); ERROR("failed to compile the regex '%s'", my_args.name);
free(regexp); rc_main = 1;
return 1; goto error;
} }
free(regexp);
fds = malloc(my_args.lxcpath_cnt * sizeof(struct pollfd)); fds = malloc(my_args.lxcpath_cnt * sizeof(struct pollfd));
if (!fds) { if (!fds) {
SYSERROR("out of memory"); SYSERROR("out of memory");
return -1; rc_main = -1;
goto error;
} }
nfds = my_args.lxcpath_cnt; nfds = my_args.lxcpath_cnt;
...@@ -147,8 +161,9 @@ int main(int argc, char *argv[]) ...@@ -147,8 +161,9 @@ int main(int argc, char *argv[])
fd = lxc_monitor_open(my_args.lxcpath[i]); fd = lxc_monitor_open(my_args.lxcpath[i]);
if (fd < 0) { if (fd < 0) {
regfree(&preg); close_fds(fds, i);
return 1; rc_main = 1;
goto cleanup;
} }
fds[i].fd = fd; fds[i].fd = fd;
fds[i].events = POLLIN; fds[i].events = POLLIN;
...@@ -159,8 +174,8 @@ int main(int argc, char *argv[]) ...@@ -159,8 +174,8 @@ int main(int argc, char *argv[])
for (;;) { for (;;) {
if (lxc_monitor_read_fdset(fds, nfds, &msg, -1) < 0) { if (lxc_monitor_read_fdset(fds, nfds, &msg, -1) < 0) {
regfree(&preg); rc_main = 1;
return 1; goto close_and_clean;
} }
msg.name[sizeof(msg.name)-1] = '\0'; msg.name[sizeof(msg.name)-1] = '\0';
...@@ -182,7 +197,15 @@ int main(int argc, char *argv[]) ...@@ -182,7 +197,15 @@ int main(int argc, char *argv[])
} }
} }
close_and_clean:
close_fds(fds, nfds);
cleanup:
regfree(&preg); regfree(&preg);
free(fds);
return 0; error:
free(regexp);
return rc_main;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment