From d53a820b375aa09403ac16f41cae4a51f955445c Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Wed, 20 Jun 2012 20:54:00 +0400 Subject: [PATCH] files-reg: Eliminate memory leak in open_remap_ghost on errors xmalloc'ed gf and gf->path are not freed if something failed. Not a big deal since we're ususally interrupt program execution on error and do exit, but anyway. Signed-off-by: Cyrill Gorcunov Signed-off-by: Pavel Emelyanov --- files-reg.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/files-reg.c b/files-reg.c index 8a267541f..24e3be23f 100644 --- a/files-reg.c +++ b/files-reg.c @@ -84,29 +84,29 @@ static int open_remap_ghost(struct reg_file_info *rfi, return -1; gf->path = xmalloc(PATH_MAX); if (!gf->path) - return -1; + goto err; ifd = open_image_ro(CR_FD_GHOST_FILE, rfe->remap_id); if (ifd < 0) - return -1; + goto err; if (read_img(ifd, &gfe) < 0) - return -1; + goto err; snprintf(gf->path, PATH_MAX, "%s.cr.%x.ghost", rfi->path, rfe->remap_id); gfd = open(gf->path, O_WRONLY | O_CREAT | O_EXCL, gfe.mode); if (gfd < 0) { pr_perror("Can't open ghost file"); - return -1; + goto err; } if (fchown(gfd, gfe.uid, gfe.gid) < 0) { pr_perror("Can't reset user/group on ghost %#x\n", rfe->remap_id); - return -1; + goto err; } if (copy_file(ifd, gfd, 0) < 0) - return -1; + goto err; close(ifd); close(gfd); @@ -116,6 +116,11 @@ static int open_remap_ghost(struct reg_file_info *rfi, gf_found: rfi->remap_path = gf->path; return 0; + +err: + xfree(gf->path); + xfree(gf); + return -1; } static int collect_remaps(void)