Commit d53a820b authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Pavel Emelyanov

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: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 94789746
...@@ -84,29 +84,29 @@ static int open_remap_ghost(struct reg_file_info *rfi, ...@@ -84,29 +84,29 @@ static int open_remap_ghost(struct reg_file_info *rfi,
return -1; return -1;
gf->path = xmalloc(PATH_MAX); gf->path = xmalloc(PATH_MAX);
if (!gf->path) if (!gf->path)
return -1; goto err;
ifd = open_image_ro(CR_FD_GHOST_FILE, rfe->remap_id); ifd = open_image_ro(CR_FD_GHOST_FILE, rfe->remap_id);
if (ifd < 0) if (ifd < 0)
return -1; goto err;
if (read_img(ifd, &gfe) < 0) if (read_img(ifd, &gfe) < 0)
return -1; goto err;
snprintf(gf->path, PATH_MAX, "%s.cr.%x.ghost", rfi->path, rfe->remap_id); 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); gfd = open(gf->path, O_WRONLY | O_CREAT | O_EXCL, gfe.mode);
if (gfd < 0) { if (gfd < 0) {
pr_perror("Can't open ghost file"); pr_perror("Can't open ghost file");
return -1; goto err;
} }
if (fchown(gfd, gfe.uid, gfe.gid) < 0) { if (fchown(gfd, gfe.uid, gfe.gid) < 0) {
pr_perror("Can't reset user/group on ghost %#x\n", rfe->remap_id); 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) if (copy_file(ifd, gfd, 0) < 0)
return -1; goto err;
close(ifd); close(ifd);
close(gfd); close(gfd);
...@@ -116,6 +116,11 @@ static int open_remap_ghost(struct reg_file_info *rfi, ...@@ -116,6 +116,11 @@ static int open_remap_ghost(struct reg_file_info *rfi,
gf_found: gf_found:
rfi->remap_path = gf->path; rfi->remap_path = gf->path;
return 0; return 0;
err:
xfree(gf->path);
xfree(gf);
return -1;
} }
static int collect_remaps(void) static int collect_remaps(void)
......
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