public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH v2] tools/mxsimage: Remove fclose on empty FILE pointer
@ 2021-11-24 12:10 Mattias Hansson
  2021-11-24 12:22 ` Wolfgang Denk
  2022-02-05 16:38 ` sbabic
  0 siblings, 2 replies; 3+ messages in thread
From: Mattias Hansson @ 2021-11-24 12:10 UTC (permalink / raw)
  To: u-boot; +Cc: Mattias Hansson

If `sb_load_cmdfile()` fails to open the configuration file it will jump
to error handling where the code will try to `fclose()` the FILE pointer
which is NULL causing `mkimage` to segfault.

This patch removes the label for error handling and instead returns
immediately which skips the `fclose()` and prevents the segfault. The
errno is also described in the error message to guide users.

Signed-off-by: Mattias Hansson <hansson.mattias@gmail.com>
---
 tools/mxsimage.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/tools/mxsimage.c b/tools/mxsimage.c
index 002f4b525a..fee022aab4 100644
--- a/tools/mxsimage.c
+++ b/tools/mxsimage.c
@@ -1595,8 +1595,11 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx)
 	size_t len;
 
 	fp = fopen(ictx->cfg_filename, "r");
-	if (!fp)
-		goto err_file;
+	if (!fp) {
+		fprintf(stderr, "ERR: Failed to load file \"%s\": \"%s\"\n",
+			ictx->cfg_filename, strerror(errno));
+		return -EINVAL;
+	}
 
 	while ((rlen = getline(&line, &len, fp)) > 0) {
 		memset(&cmd, 0, sizeof(cmd));
@@ -1616,12 +1619,6 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx)
 	fclose(fp);
 
 	return 0;
-
-err_file:
-	fclose(fp);
-	fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
-		ictx->cfg_filename);
-	return -EINVAL;
 }
 
 static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx)
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] tools/mxsimage: Remove fclose on empty FILE pointer
  2021-11-24 12:10 [PATCH v2] tools/mxsimage: Remove fclose on empty FILE pointer Mattias Hansson
@ 2021-11-24 12:22 ` Wolfgang Denk
  2022-02-05 16:38 ` sbabic
  1 sibling, 0 replies; 3+ messages in thread
From: Wolfgang Denk @ 2021-11-24 12:22 UTC (permalink / raw)
  To: Mattias Hansson; +Cc: u-boot

Dear Mattias Hansson,

In message <20211124121049.24054-1-hansson.mattias@gmail.com> you wrote:
> If `sb_load_cmdfile()` fails to open the configuration file it will jump
> to error handling where the code will try to `fclose()` the FILE pointer
> which is NULL causing `mkimage` to segfault.
>
> This patch removes the label for error handling and instead returns
> immediately which skips the `fclose()` and prevents the segfault. The
> errno is also described in the error message to guide users.
>
> Signed-off-by: Mattias Hansson <hansson.mattias@gmail.com>

Reviewed-by: Wolfgang Denk <wd@denx.de>

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de
Time is a drug. Too much of it kills you.
                                      - Terry Pratchett, _Small Gods_

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] tools/mxsimage: Remove fclose on empty FILE pointer
  2021-11-24 12:10 [PATCH v2] tools/mxsimage: Remove fclose on empty FILE pointer Mattias Hansson
  2021-11-24 12:22 ` Wolfgang Denk
@ 2022-02-05 16:38 ` sbabic
  1 sibling, 0 replies; 3+ messages in thread
From: sbabic @ 2022-02-05 16:38 UTC (permalink / raw)
  To: Mattias Hansson, u-boot

> If `sb_load_cmdfile()` fails to open the configuration file it will jump
> to error handling where the code will try to `fclose()` the FILE pointer
> which is NULL causing `mkimage` to segfault.
> This patch removes the label for error handling and instead returns
> immediately which skips the `fclose()` and prevents the segfault. The
> errno is also described in the error message to guide users.
> Signed-off-by: Mattias Hansson <hansson.mattias@gmail.com>
> Reviewed-by: Wolfgang Denk <wd@denx.de>
Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-02-05 16:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-24 12:10 [PATCH v2] tools/mxsimage: Remove fclose on empty FILE pointer Mattias Hansson
2021-11-24 12:22 ` Wolfgang Denk
2022-02-05 16:38 ` sbabic

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox