[CRIU] [PATCH] image: close images on error paths

Andrey Vagin avagin at openvz.org
Tue Jan 20 05:19:14 PST 2015


Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 bfd.c   |  2 +-
 image.c | 11 +++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/bfd.c b/bfd.c
index f89a9df..e4cccbb 100644
--- a/bfd.c
+++ b/bfd.c
@@ -125,7 +125,7 @@ void bclose(struct bfd *f)
 
 		buf_put(&f->b);
 	}
-	close(f->fd);
+	close_safe(&f->fd);
 }
 
 static int brefill(struct bfd *f)
diff --git a/image.c b/image.c
index 36f726d..e60cda7 100644
--- a/image.c
+++ b/image.c
@@ -236,7 +236,7 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...)
 	if (oflags & O_NOBUF)
 		bfd_setraw(&img->_x);
 	else if (bfdopen(&img->_x, flags))
-		goto err;
+		goto err_close;
 
 	if (imgset_template[type].magic == RAW_IMAGE_MAGIC)
 		goto skip_magic;
@@ -245,14 +245,14 @@ struct cr_img *open_image_at(int dfd, int type, unsigned long flags, ...)
 		u32 magic;
 
 		if (read_img(img, &magic) < 0)
-			goto err;
+			goto err_close;
 		if (magic != imgset_template[type].magic) {
 			pr_err("Magic doesn't match for %s\n", path);
-			goto err;
+			goto err_close;
 		}
 	} else {
 		if (write_img(img, &imgset_template[type].magic))
-			goto err;
+			goto err_close;
 	}
 
 skip_magic:
@@ -262,6 +262,9 @@ err:
 	xfree(img);
 errn:
 	return NULL;
+err_close:
+	close_image(img);
+	return NULL;
 }
 
 void close_image(struct cr_img *img)
-- 
1.9.3



More information about the CRIU mailing list