From: Igor Sukhih Date: Tue, 22 Aug 2006 13:39:35 +0000 (+0400) Subject: Fixed memory leaks in realloc() X-Git-Url: http://git.openvz.org/?p=vzctl;a=commitdiff;h=48bada8af358255c7b54f437ba0002eabf0df368 Fixed memory leaks in realloc() --- Index: vzctl-3.0.11/src/lib/list.c =================================================================== --- vzctl-3.0.11.orig/src/lib/list.c +++ vzctl-3.0.11/src/lib/list.c @@ -24,7 +24,7 @@ char *list2str_c(char *name, char c, list_head_t *head) { - char *buf = NULL; + char *buf = NULL, *tmp; int buf_len, len, r; char *sp, *ep; const int delta = 256; @@ -61,9 +61,12 @@ char *list2str_c(char *name, char c, lis int cur_len = sp - buf; buf_len += delta > len ? delta : len + 1; - buf = realloc(buf, buf_len); - if (buf == NULL) + tmp = realloc(buf, buf_len); + if (tmp == NULL) { + free(buf); return NULL; + } + buf = tmp; ep = buf + buf_len; sp = buf + cur_len; } Index: vzctl-3.0.11/src/lib/net.c =================================================================== --- vzctl-3.0.11.orig/src/lib/net.c +++ vzctl-3.0.11/src/lib/net.c @@ -349,7 +349,7 @@ static inline int get_vps_ip_ioctl(vps_h { int ret = -1; struct vzlist_veipv4ctl veip; - uint32_t *addr; + uint32_t *addr, *tmp; char buf[16]; int i; @@ -366,9 +366,12 @@ static inline int get_vps_ip_ioctl(vps_h else if (ret <= veip.num) break; veip.num = ret; - addr = realloc(addr, veip.num * sizeof(*veip.ip)); - if (addr == NULL) - return -1; + tmp = realloc(addr, veip.num * sizeof(*veip.ip)); + if (tmp == NULL) { + ret = -1; + goto out; + } + addr = tmp; } if (ret > 0) { for (i = ret - 1; i >= 0; i--) { Index: vzctl-3.0.11/src/lib/script.c =================================================================== --- vzctl-3.0.11.orig/src/lib/script.c +++ vzctl-3.0.11/src/lib/script.c @@ -42,7 +42,7 @@ static char *envp_bash[] = {"HOME=/", "T int read_script(const char *fname, char *include, char **buf) { struct stat st; - char *p = NULL; + char *tmp, *p = NULL; int fd, len = 0; char *inc; @@ -74,9 +74,10 @@ int read_script(const char *fname, char goto err; } if (*buf != NULL) { - *buf = realloc(*buf, st.st_size + len + 2); - if (*buf == NULL) + tmp = realloc(*buf, st.st_size + len + 2); + if (tmp == NULL) goto err; + *buf = tmp; p = *buf + len; } else { *buf = malloc(st.st_size + 2); Index: vzctl-3.0.11/src/vzlist.c =================================================================== --- vzctl-3.0.11.orig/src/vzlist.c +++ vzctl-3.0.11/src/vzlist.c @@ -581,11 +581,13 @@ void *x_malloc(int size) void *x_realloc(void *ptr, int size) { - if ((ptr = realloc(ptr, size)) == NULL) { + void *tmp; + + if ((tmp = realloc(ptr, size)) == NULL) { printf("Error: unable to allocate %d bytes\n", size); exit(1); } - return ptr; + return tmp; } void usage() @@ -1637,5 +1639,9 @@ int main(int argc, char **argv) return ret; print_ve(); free_veinfo(); + if (host_pattern != NULL) free(host_pattern); + if (name_pattern != NULL) free(name_pattern); + if (f_order != NULL) free(f_order); + return 0; }