[PATCH] mkimage: fit: Simplify tmpfile name calculation

Sean Anderson sean.anderson at seco.com
Fri Apr 8 21:45:53 CEST 2022


snprintf will not overrun the buffer, and will return the number of
characters which would have been printed (had the buffer been large
enough). This allows us to create the tmpfile name and check for
overflow in one pass.

Signed-off-by: Sean Anderson <sean.anderson at seco.com>
---

 tools/fit_image.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/tools/fit_image.c b/tools/fit_image.c
index 1884a2eb0b..0d5a6a28f9 100644
--- a/tools/fit_image.c
+++ b/tools/fit_image.c
@@ -684,14 +684,13 @@ static int fit_handle_file(struct image_tool_params *params)
 	debug ("FIT format handling\n");
 
 	/* call dtc to include binary properties into the tmp file */
-	if (strlen (params->imagefile) +
-		strlen (MKIMAGE_TMPFILE_SUFFIX) + 1 > sizeof (tmpfile)) {
+	if (snprintf(tmpfile, sizeof(tmpfile), "%s%s", params->imagefile,
+		     MKIMAGE_TMPFILE_SUFFIX) >= sizeof(tmpfile)) {
 		fprintf (stderr, "%s: Image file name (%s) too long, "
 				"can't create tmpfile.\n",
 				params->imagefile, params->cmdname);
 		return (EXIT_FAILURE);
 	}
-	sprintf (tmpfile, "%s%s", params->imagefile, MKIMAGE_TMPFILE_SUFFIX);
 
 	/* We either compile the source file, or use the existing FIT image */
 	if (params->auto_its) {
-- 
2.35.1.1320.gc452695387.dirty



More information about the U-Boot mailing list