[CRIU] [PATCH 7/9] files-reg: don't forget to close fd in case of error

Andrey Vagin avagin at openvz.org
Thu Apr 4 17:44:33 EDT 2013


CID 996201 (#1 of 1): Resource leak (RESOURCE_LEAK)
9. leaked_handle: Handle variable "fd" going out of scope leaks the handle.

CID 996202 (#1 of 1): Resource leak (RESOURCE_LEAK)
6. leaked_handle: Handle variable "fd" going out of scope leaks the handle.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 files-reg.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/files-reg.c b/files-reg.c
index 2726b8e..8aff9e7 100644
--- a/files-reg.c
+++ b/files-reg.c
@@ -231,7 +231,7 @@ static int dump_ghost_file(int _fd, u32 id, const struct stat *st)
 		return -1;
 
 	if (S_ISREG(st->st_mode)) {
-		int fd;
+		int fd, ret;
 
 		/*
 		 * Reopen file locally since it may have no read
@@ -243,10 +243,10 @@ static int dump_ghost_file(int _fd, u32 id, const struct stat *st)
 			pr_perror("Can't open ghost original file");
 			return -1;
 		}
-		if (copy_file(fd, img, st->st_size))
-			return -1;
-
+		ret = copy_file(fd, img, st->st_size);
 		close(fd);
+		if (ret)
+			return -1;
 	}
 
 	close(img);
@@ -538,6 +538,7 @@ static int do_open_reg(struct reg_file_info *rfi, void *arg)
 
 	if (lseek(fd, rfi->rfe->pos, SEEK_SET) < 0) {
 		pr_perror("Can't restore file pos");
+		close(fd);
 		return -1;
 	}
 
-- 
1.7.11.7



More information about the CRIU mailing list