[PATCH 2/8] bootstrap: Fix potential memory leak in 'read_image_head'

Andrey Smirnov andrew.smirnov at gmail.com
Sun May 3 13:13:13 PDT 2015


Original version of 'read_image_head' would not free the memory
allocated for barebox header in cases of any failure. Fix this by
adding a dedicated resourse de-allocation code path.

Signed-off-by: Andrey Smirnov <andrew.smirnov at gmail.com>
---
 lib/bootstrap/devfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/bootstrap/devfs.c b/lib/bootstrap/devfs.c
index 704680a..82c7d21 100644
--- a/lib/bootstrap/devfs.c
+++ b/lib/bootstrap/devfs.c
@@ -35,7 +35,7 @@ static void *read_image_head(const char *name)
 	cdev = cdev_open(name, O_RDONLY);
 	if (!cdev) {
 		bootstrap_err("failed to open partition\n");
-		return NULL;
+		goto free_header;
 	}

 	ret = cdev_read(cdev, header, BAREBOX_HEAD_SIZE, 0, 0);
@@ -43,10 +43,14 @@ static void *read_image_head(const char *name)

 	if (ret != BAREBOX_HEAD_SIZE) {
 		bootstrap_err("failed to read from partition\n");
-		return NULL;
+		goto free_header;
 	}

 	return header;
+
+free_header:
+	free(header);
+	return NULL;
 }

 static unsigned int get_image_size(void *head)
--
2.1.4



More information about the barebox mailing list