From: Wolfgang Denk <wd@denx.de>
To: Mattias Hansson <hansson.mattias@gmail.com>
Cc: u-boot@lists.denx.de
Subject: Re: [PATCH] tools/mxsimage: Remove fclose on empty FILE pointer
Date: Wed, 24 Nov 2021 09:16:24 +0100 [thread overview]
Message-ID: <2787774.1637741784@gemini.denx.de> (raw)
In-Reply-To: <20211123080633.8318-1-hansson.mattias@gmail.com>
Dear Mattias Hansson,
In message <20211123080633.8318-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 `fclose()` since `fopen()` always returns NULL
> instead of the file descriptor when failing.
> ---
> tools/mxsimage.c | 1 -
> 1 file changed, 1 deletion(-)
>
> diff --git a/tools/mxsimage.c b/tools/mxsimage.c
> index 002f4b525a..c7bd86ce52 100644
> --- a/tools/mxsimage.c
> +++ b/tools/mxsimage.c
> @@ -1618,7 +1618,6 @@ static int sb_load_cmdfile(struct sb_image_ctx *ictx)
> return 0;
>
> err_file:
> - fclose(fp);
> fprintf(stderr, "ERR: Failed to load file \"%s\"\n",
> ictx->cfg_filename);
> return -EINVAL;
The whole code with the goto is ugly. Such an error exit may make
sense when used in several places, but here it just makes reading
the code more difficult. I suggest to fix this like this:
diff --git a/tools/mxsimage.c b/tools/mxsimage.c
index 002f4b525a..801aaa62ce 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\"\n",
+ ictx->cfg_filename);
+ 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)
Thanks.
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
Applying computer technology is simply finding the right wrench to
pound in the correct screw.
next prev parent reply other threads:[~2021-11-24 8:16 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-23 8:06 [PATCH] tools/mxsimage: Remove fclose on empty FILE pointer Mattias Hansson
2021-11-23 19:12 ` Fabio Estevam
2021-11-24 8:16 ` Wolfgang Denk [this message]
2021-11-24 8:35 ` Mattias Hansson
-- strict thread matches above, loose matches on Subject: below --
2021-11-24 7:29 Mattias Hansson
2021-11-19 15:44 Mattias Hansson
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=2787774.1637741784@gemini.denx.de \
--to=wd@denx.de \
--cc=hansson.mattias@gmail.com \
--cc=u-boot@lists.denx.de \
/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