[CRIU] [PATCH 6/9] cr-restore: fix error handling and memory leak in read_vma

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


CID 996203 (#1 of 1): Resource leak (RESOURCE_LEAK)
15. leaked_storage: Variable "vma" going out of scope leaks the storage it points to.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 cr-restore.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index 82fa577..48b441b 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -445,28 +445,33 @@ static int read_vmas(int pid)
 			break;
 
 		ret = pb_read_one_eof(fd, &e, PB_VMAS);
-		if (ret <= 0)
+		if (ret <= 0) {
+			xfree(vma);
 			break;
+		}
 
 		rst_vmas.nr++;
 		list_add_tail(&vma->list, &rst_vmas.h);
 
-		if (e->fd != -1) {
+		vma->vma = *e;
+		vma_entry__free_unpacked(e, NULL);
+
+		if (vma->vma.fd != -1) {
 			ret = -1;
 			pr_err("Error in vma->fd setting (%Ld)\n",
-					(unsigned long long)e->fd);
+					(unsigned long long)vma->vma.fd);
 			break;
 		}
 
-		vma->vma = *e;
-		vma_entry__free_unpacked(e, NULL);
-
 		if (!vma_priv(&vma->vma))
 			continue;
 
 		priv_size += vma_area_len(vma);
 	}
 
+	if (ret < 0)
+		goto out;
+
 	/* Reserve a place for mapping private vma-s one by one */
 	addr = mmap(NULL, priv_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
 	if (addr == MAP_FAILED) {
-- 
1.7.11.7



More information about the CRIU mailing list