mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] fip: fix missing close() in error path
Date: Mon,  3 Mar 2025 08:28:16 +0100	[thread overview]
Message-ID: <20250303072816.3789920-1-s.hauer@pengutronix.de> (raw)

The file opened in fip_read_image_from_file() is never closed in the
error paths. Add the missing close() and while at it free the allocated
pointer as well.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/fip.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/fip.c b/lib/fip.c
index 23e82098da..99d6c5c383 100644
--- a/lib/fip.c
+++ b/lib/fip.c
@@ -284,7 +284,7 @@ int fip_parse(struct fip_state *fip,
 static struct fip_image *fip_read_image_from_file(const uuid_t *uuid, const char *filename)
 {
 	struct stat st;
-	struct fip_image *image;
+	struct fip_image *image = NULL;
 	int fd;
 
 	ASSERT(uuid != NULL);
@@ -298,7 +298,7 @@ static struct fip_image *fip_read_image_from_file(const uuid_t *uuid, const char
 
 	if (fstat(fd, &st) == -1) {
 		pr_err("fstat %s: %m\n", filename);
-		return NULL;
+		goto err;
 	}
 
 	image = xzalloc(sizeof(*image));
@@ -306,12 +306,18 @@ static struct fip_image *fip_read_image_from_file(const uuid_t *uuid, const char
 	image->buffer = xmalloc(st.st_size);
 	if (read_full(fd, image->buffer, st.st_size) != st.st_size) {
 		pr_err("Failed to read %s: %m\n", filename);
-		return NULL;
+		goto err;
 	}
 	image->toc_e.size = st.st_size;
 
 	close(fd);
 	return image;
+
+err:
+	close(fd);
+	free(image);
+
+	return NULL;
 }
 
 int fip_pack_images(struct fip_state *fip,
-- 
2.39.5




                 reply	other threads:[~2025-03-03  7:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250303072816.3789920-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox