[PATCH kexec-tools 1/3] uImage: use goto for unwind in uImage_gz_load

Simon Horman horms at kernel.org
Tue Nov 10 03:35:16 PST 2026


In the case where an error requires uncmp_buf to be freed,
which is many cases, use a goto label rather than repeatedly
open-coding this clean-up.

This is in preparation for adding more such cases.

Signed-off-by: Simon Horman <horms at kernel.org>
---
 kexec/kexec-uImage.c | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/kexec/kexec-uImage.c b/kexec/kexec-uImage.c
index 016be105646b..76b8c73a3494 100644
--- a/kexec/kexec-uImage.c
+++ b/kexec/kexec-uImage.c
@@ -157,16 +157,13 @@ static int uImage_gz_load(const char *buf, off_t len,
 	skip = 10;
 
 	/* check GZ magic */
-	if (buf[0] != 0x1f || buf[1] != 0x8b) {
-		free(uncomp_buf);
-		return -1;
-	}
+	if (buf[0] != 0x1f || buf[1] != 0x8b)
+		goto err_free;
 
 	flags = buf[3];
 	if (buf[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
 		puts ("Error: Bad gzipped data\n");
-		free(uncomp_buf);
-		return -1;
+		goto err_free;
 	}
 
 	if (flags & EXTRA_FIELD) {
@@ -190,10 +187,8 @@ static int uImage_gz_load(const char *buf, off_t len,
 
 	/* - activates parsing gz headers */
 	ret = inflateInit2(&strm, -MAX_WBITS);
-	if (ret != Z_OK) {
-		free(uncomp_buf);
-		return -1;
-	}
+	if (ret != Z_OK)
+		goto err_free;
 
 	strm.next_out = uncomp_buf;
 	strm.avail_out = mem_alloc;
@@ -211,17 +206,15 @@ static int uImage_gz_load(const char *buf, off_t len,
 			new_buf = realloc(uncomp_buf, mem_alloc);
 			if (!new_buf) {
 				inflateEnd(&strm);
-				free(uncomp_buf);
-				return -1;
+				goto err_free;
 			}
 
 			uncomp_buf = new_buf;
 			strm.next_out = uncomp_buf + mem_alloc - inc_buf;
 			strm.avail_out = inc_buf;
 		} else {
-			free(uncomp_buf);
 			printf("Error during decompression %d\n", ret);
-			return -1;
+			goto err_free;
 		}
 	} while (1);
 
@@ -229,6 +222,10 @@ static int uImage_gz_load(const char *buf, off_t len,
 	image->buf = (char *)uncomp_buf;
 	image->len = mem_alloc - strm.avail_out;
 	return 0;
+
+err_free:
+	free(uncomp_buf);
+	return -1;
 }
 #else
 static int uImage_gz_load(const char *UNUSED(buf), off_t UNUSED(len),

-- 
2.56.0




More information about the kexec mailing list