From 2c12120713694e36ee26e4f7b19e7121cd70b493 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Fri, 29 Apr 2016 15:36:14 +0800 Subject: [PATCH 1/2] fix two strdup memory leak Signed-off-by: Peng Tao --- src/util.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/util.c b/src/util.c index e30e913..307789a 100644 --- a/src/util.c +++ b/src/util.c @@ -111,7 +111,8 @@ void hyper_sync_time_hctosys() { } } -int hyper_find_sd(char *addr, char **dev) { +int hyper_find_sd(char *addr, char **dev) +{ struct dirent **list; struct dirent *dir; char path[512]; @@ -152,30 +153,30 @@ int hyper_mkdir(char *hyper_path) if (path == NULL) { errno = ENOMEM; - return -1; + goto fail; } if (stat(path, &st) >= 0) { if (S_ISDIR(st.st_mode)) - return 0; + goto out; errno = ENOTDIR; - return -1; + goto fail; } if (errno != ENOENT) - return -1; + goto fail; p = strrchr(path, '/'); if (p == NULL) { errno = EINVAL; - return -1; + goto fail; } if (p != path) { *p = '\0'; if (hyper_mkdir(path) < 0) - return -1; + goto fail; *p = '/'; } @@ -183,10 +184,15 @@ int hyper_mkdir(char *hyper_path) fprintf(stdout, "create directory %s\n", path); if (mkdir(path, 0755) < 0 && errno != EEXIST) { perror("failed to create directory"); - return -1; + goto fail; } - +out: + free(path); return 0; + +fail: + free(path); + return -1; } void online_cpu(void) @@ -519,6 +525,8 @@ static void hyper_unmount_all(void) fprintf(stdout, ("umount %s: %s failed\n"), filesys, strerror(errno)); } + free(filesys); + mntlist[i] = NULL; } sync(); From 4475e2db1b616047e2084e15810bcf34b8554ecf Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 20 Apr 2016 21:16:31 +0800 Subject: [PATCH 2/2] fix possible memory leak We are overriding container->image when passed container->scsiaddr. Make sure we also free its memory if caller also passes the image field. Signed-off-by: Peng Tao --- src/container.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/container.c b/src/container.c index c0e7f54..a7eae46 100644 --- a/src/container.c +++ b/src/container.c @@ -523,8 +523,10 @@ static int hyper_container_init(void *data) char dev[128]; char *options = NULL; - if (container->scsiaddr) + if (container->scsiaddr) { + free(container->image); hyper_find_sd(container->scsiaddr, &container->image); + } sprintf(dev, "/dev/%s", container->image); fprintf(stdout, "device %s\n", dev);