Anton Arapov 0a7886
From 458f73d58c24a7addce82bf1e8bfb8c2554ca458 Mon Sep 17 00:00:00 2001
Anton Arapov 0a7886
From: Jean Delvare <jdelvare@suse.de>
Anton Arapov 0a7886
Date: Wed, 14 Oct 2015 14:37:09 +0200
Anton Arapov 0a7886
Subject: [PATCH 3/9] Fix error paths in mem_chunk
Anton Arapov 0a7886
Anton Arapov 0a7886
Use a common error path in function mem_chunk, to make sure it does
Anton Arapov 0a7886
not leak memory and does not leave an opened file descriptor behind,
Anton Arapov 0a7886
without duplicating the cleaning code.
Anton Arapov 0a7886
---
Anton Arapov 0a7886
 CHANGELOG |  1 +
Anton Arapov 0a7886
 util.c    | 24 ++++++++++--------------
Anton Arapov 0a7886
 2 files changed, 11 insertions(+), 14 deletions(-)
Anton Arapov 0a7886
Anton Arapov 0a7886
diff --git a/CHANGELOG b/CHANGELOG
Anton Arapov 0a7886
index aa1c28f..c940c9f 100644
Anton Arapov 0a7886
--- a/CHANGELOG
Anton Arapov 0a7886
+++ b/CHANGELOG
Anton Arapov 0a7886
@@ -3,6 +3,7 @@
Anton Arapov 0a7886
 	* util.c: Avoid SIGBUS on mmap failure.
Anton Arapov 0a7886
 	  This fixes Savannah bug #46066:
Anton Arapov 0a7886
 	  https://savannah.nongnu.org/bugs/?46066
Anton Arapov 0a7886
+	* util.c: Fix error paths in mem_chunk.
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 2015-10-01  Roy Franz  <roy.franz@linaro.org>
Anton Arapov 0a7886
 
Anton Arapov 0a7886
diff --git a/util.c b/util.c
Anton Arapov 0a7886
index 5795d02..f97ac0d 100644
Anton Arapov 0a7886
--- a/util.c
Anton Arapov 0a7886
+++ b/util.c
Anton Arapov 0a7886
@@ -166,7 +166,7 @@ void *mem_chunk(off_t base, size_t len, const char *devmem)
Anton Arapov 0a7886
 	if ((p = malloc(len)) == NULL)
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
 		perror("malloc");
Anton Arapov 0a7886
-		return NULL;
Anton Arapov 0a7886
+		goto out;
Anton Arapov 0a7886
 	}
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 #ifdef USE_MMAP
Anton Arapov 0a7886
@@ -174,8 +174,7 @@ void *mem_chunk(off_t base, size_t len, const char *devmem)
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
 		fprintf(stderr, "%s: ", devmem);
Anton Arapov 0a7886
 		perror("stat");
Anton Arapov 0a7886
-		free(p);
Anton Arapov 0a7886
-		return NULL;
Anton Arapov 0a7886
+		goto err_free;
Anton Arapov 0a7886
 	}
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 	/*
Anton Arapov 0a7886
@@ -186,8 +185,7 @@ void *mem_chunk(off_t base, size_t len, const char *devmem)
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
 		fprintf(stderr, "mmap: Can't map beyond end of file %s\n",
Anton Arapov 0a7886
 			devmem);
Anton Arapov 0a7886
-		free(p);
Anton Arapov 0a7886
-		return NULL;
Anton Arapov 0a7886
+		goto err_free;
Anton Arapov 0a7886
 	}
Anton Arapov 0a7886
 
Anton Arapov 0a7886
 #ifdef _SC_PAGESIZE
Anton Arapov 0a7886
@@ -220,19 +218,17 @@ try_read:
Anton Arapov 0a7886
 	{
Anton Arapov 0a7886
 		fprintf(stderr, "%s: ", devmem);
Anton Arapov 0a7886
 		perror("lseek");
Anton Arapov 0a7886
-		free(p);
Anton Arapov 0a7886
-		return NULL;
Anton Arapov 0a7886
+		goto err_free;
Anton Arapov 0a7886
 	}
Anton Arapov 0a7886
 
Anton Arapov 0a7886
-	if (myread(fd, p, len, devmem) == -1)
Anton Arapov 0a7886
-	{
Anton Arapov 0a7886
-		free(p);
Anton Arapov 0a7886
-		return NULL;
Anton Arapov 0a7886
-	}
Anton Arapov 0a7886
+	if (myread(fd, p, len, devmem) == 0)
Anton Arapov 0a7886
+		goto out;
Anton Arapov 0a7886
+
Anton Arapov 0a7886
+err_free:
Anton Arapov 0a7886
+	free(p);
Anton Arapov 0a7886
+	p = NULL;
Anton Arapov 0a7886
 
Anton Arapov 0a7886
-#ifdef USE_MMAP
Anton Arapov 0a7886
 out:
Anton Arapov 0a7886
-#endif
Anton Arapov 0a7886
 	if (close(fd) == -1)
Anton Arapov 0a7886
 		perror(devmem);
Anton Arapov 0a7886
 
Anton Arapov 0a7886
-- 
Anton Arapov 0a7886
2.5.0
Anton Arapov 0a7886