[CRIU] [crtools-bot for Cyrill Gorcunov ] restore: Don't leak opened file descriptor in shmem_remap

Cyrill Gorcunov gorcunov at openvz.org
Mon Feb 20 05:23:28 EST 2012


The commit is pushed to "master" and will appear on git://github.com/cyrillos/crtools.git
------>
commit 6a0b94e7d563a725532f2f19d02197b712101f5f
Author: Cyrill Gorcunov <gorcunov at openvz.org>
Date:   Fri Feb 17 11:33:15 2012 +0400

    restore: Don't leak opened file descriptor in shmem_remap
    
    Don't forget to close opened file in case of error.
    
    Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
    Acked-by: Pavel Emelyanov <xemul at parallels.com>
---
 cr-restore.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index 38d8b4f..8208509 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -343,8 +343,7 @@ static int prepare_pipes_pid(int pid)
 static int shmem_remap(void *old_addr, void *new_addr, unsigned long size)
 {
 	char path[PATH_MAX];
-	int fd;
-	void *ret;
+	int fd, ret = -1;
 
 	sprintf(path, "/proc/self/map_files/%lx-%lx",
 		(long)old_addr, (long)old_addr + size);
@@ -355,15 +354,17 @@ static int shmem_remap(void *old_addr, void *new_addr, unsigned long size)
 		return -1;
 	}
 
-	ret = mmap(new_addr, size, PROT_READ | PROT_WRITE,
-		   MAP_SHARED | MAP_FIXED, fd, 0);
-	if (ret != new_addr) {
+	/* reuse old_addr variable */
+	old_addr = mmap(new_addr, size, PROT_READ | PROT_WRITE,
+			MAP_SHARED | MAP_FIXED, fd, 0);
+	if (new_addr == old_addr)
+		ret = 0;
+	else
 		pr_perror("mmap failed");
-		return -1;
-	}
 
 	close(fd);
-	return 0;
+
+	return ret;
 }
 
 static int prepare_shared(int ps_fd)


More information about the CRIU mailing list