Skip to contentSkip to conversation

tools/mxsimage: Remove fclose on empty FILE pointer

PATCH In review Mattias Hansson 3 participants 3 messages Nov 24, 2021 – Feb 5, 2022 Arm i.MX and Layerscape

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/mxsimage: Remove fclose on empty FILE pointer

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.

---
 tools/mxsimage.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
tools/mxsimage.c +5 -8
index 002f4b525a..fee022aab4 100644
@@ -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)
Signature · 1 line
2.27.0
Reviewed-by on v2 1/1
#1 lore raw

Dear Mattias Hansson,

Quoted 9 lines · In message <[email protected]> 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.

Best regards,

Wolfgang Denk

Signature · 5 lines
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: [email protected]
Time is a drug. Too much of it kills you.
                                      - Terry Pratchett, _Small Gods_
#2 lore raw

PATCH v2 1/1 tools/mxsimage: Remove fclose on empty FILE pointer

In reply to Mattias Hansson
Quoted 8 lines

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.

Applied to u-boot-imx, master, thanks !

Best regards,
Stefano Babic

Signature · 5 lines
=====================================================================
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: [email protected]
=====================================================================