From 7f43f4fadecb9035888af5125afcb791bbf52872 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Fri, 10 Feb 2017 20:47:40 +0530 Subject: glfs: fix memory leak in blockStuffMetaInfo Signed-off-by: Pranith Kumar K Reviewed-by: Prasanna Kumar Kalever Signed-off-by: Prasanna Kumar Kalever --- rpc/glfs-operations.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'rpc/glfs-operations.c') diff --git a/rpc/glfs-operations.c b/rpc/glfs-operations.c index 9d687a4..2fe4631 100644 --- a/rpc/glfs-operations.c +++ b/rpc/glfs-operations.c @@ -209,12 +209,13 @@ blockFreeMetaInfo(MetaInfo *info) } -static void +static int blockStuffMetaInfo(MetaInfo *info, char *line) { char *tmp = strdup(line); char *opt = strtok(tmp, ":"); - bool Flag = 0; + bool flag = 0; + int ret = -1; size_t i; @@ -238,25 +239,25 @@ blockStuffMetaInfo(MetaInfo *info, char *line) default: if(!info->list) { if(GB_ALLOC(info->list) < 0) - return; + goto out; if(GB_ALLOC(info->list[0]) < 0) - return; + goto out; strcpy(info->list[0]->addr, opt); strcpy(info->list[0]->status, strchr(line, ' ')+1); info->nhosts = 1; } else { if(GB_REALLOC_N(info->list, info->nhosts+1) < 0) - return; + goto out; for (i = 0; i < info->nhosts; i++) { if(!strcmp(info->list[i]->addr, opt)) { strcpy(info->list[i]->status, strchr(line, ' ')+1); - Flag = 1; + flag = 1; break; } } - if (!Flag) { + if (!flag) { if(GB_ALLOC(info->list[info->nhosts]) < 0) - return; + goto out; strcpy(info->list[info->nhosts]->addr, opt); strcpy(info->list[info->nhosts]->status, strchr(line, ' ')+1); info->nhosts++; @@ -265,7 +266,12 @@ blockStuffMetaInfo(MetaInfo *info, char *line) break; } + ret = 0; + + out: GB_FREE(tmp); + + return ret; } @@ -273,34 +279,41 @@ int blockGetMetaInfo(struct glfs* glfs, char* metafile, MetaInfo *info) { size_t count = 0; - struct glfs_fd *tgmfd; + struct glfs_fd *tgmfd = NULL; char line[1024]; char *tmp; - int ret; + int ret = 0; ret = glfs_chdir (glfs, GB_METADIR); if (ret) { LOG("gfapi", GB_LOG_ERROR, "glfs_chdir(%s) on volume %s failed[%s]", GB_METADIR, info->volume, strerror(errno)); - return ret; + goto out; } tgmfd = glfs_open(glfs, metafile, O_RDONLY); if (!tgmfd) { LOG("gfapi", GB_LOG_ERROR, "glfs_open(%s) on volume %s failed[%s]", metafile, info->volume, strerror(errno)); - return -1; + ret = -1; + goto out; } while (glfs_read (tgmfd, line, sizeof(line), 0) > 0) { tmp = strtok(line,"\n"); count += strlen(tmp) + 1; - blockStuffMetaInfo(info, tmp); + ret = blockStuffMetaInfo(info, tmp); + if (ret) { + goto out; + } glfs_lseek(tgmfd, count, SEEK_SET); } - glfs_close(tgmfd); + out: + if (tgmfd) { + glfs_close(tgmfd); + } - return 0; + return ret; } -- cgit