[PATCH] tools: mkimage: Fix nullptr at strchr() The 'file' pointer can be null. Thus, before using it in the strchr() function, you need to make sure that it is not null.

Fixes: 0b0c6af38738 ("Prepare v2020.01") Signed-off-by: Mikhail Ilin ilin.mikhail.ol@gmail.com --- tools/mkimage.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/tools/mkimage.c b/tools/mkimage.c index 5f51d2cc89..95005d8414 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -509,6 +509,8 @@ int main(int argc, char **argv) file = params.datafile;
for (;;) { + if (!file) + break; char *sep = strchr(file, ':'); if (sep) { *sep = '\0';

Hi,
On Tue, 22 Nov 2022 at 01:32, Mikhail Ilin ilin.mikhail.ol@gmail.com wrote:
Fixes: 0b0c6af38738 ("Prepare v2020.01") Signed-off-by: Mikhail Ilin ilin.mikhail.ol@gmail.com
tools/mkimage.c | 2 ++ 1 file changed, 2 insertions(+)
Please can you add a commit message and keep your subject to 50 chars?
For the fixes tag, can you find a commit rather than the merge thing?
diff --git a/tools/mkimage.c b/tools/mkimage.c index 5f51d2cc89..95005d8414 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -509,6 +509,8 @@ int main(int argc, char **argv) file = params.datafile;
for (;;) {
if (!file)
break; char *sep = strchr(file, ':'); if (sep) { *sep = '\0';
-- 2.17.1
Regards, SImon

The 'file' pointer can be null. Thus, before using it in the strchr() function, you need to make sure that it is not null.
Fixes: d1be8f922eb3 ("mkimage: Fix variable length header support") Signed-off-by: Mikhail Ilin ilin.mikhail.ol@gmail.com --- tools/mkimage.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/tools/mkimage.c b/tools/mkimage.c index 5f51d2cc89..95005d8414 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -509,6 +509,8 @@ int main(int argc, char **argv) file = params.datafile;
for (;;) { + if (!file) + break; char *sep = strchr(file, ':'); if (sep) { *sep = '\0';

On 11/23/22 08:10, Mikhail Ilin wrote:
The 'file' pointer can be null. Thus, before using it in the strchr() function, you need to make sure that it is not null.
Fixes: d1be8f922eb3 ("mkimage: Fix variable length header support") Signed-off-by: Mikhail Ilin ilin.mikhail.ol@gmail.com
tools/mkimage.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/tools/mkimage.c b/tools/mkimage.c index 5f51d2cc89..95005d8414 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -509,6 +509,8 @@ int main(int argc, char **argv) file = params.datafile;
for (;;) {
if (!file)
break;
The main function is now 370 lines long. It really should be restructured into smaller functions to make it readable. I would prefer if you would pull out a function copy_datafile(ifd, params.datafile) for copying the data file.
char *sep = strchr(file, ':');
We don't want variable definitions in the middle of the code. Please, move the declaration to the top of the code block.
Best regards
Heinrich
if (sep) { *sep = '\0';

The copy_datafile(ifd, params.datafile) function has been implemented to copy data by reducing the number of lines in the main function.
Signed-off-by: Mikhail Ilin ilin.mikhail.ol@gmail.com --- tools/mkimage.c | 35 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-)
diff --git a/tools/mkimage.c b/tools/mkimage.c index 30c6df7708..0b68a29285 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -430,6 +430,24 @@ static void verify_image(const struct image_type_params *tparams) (void)close(ifd); }
+void copy_datafile(int ifd, char *file) +{ + if (!file) + return; + for (;;) { + char *sep = strchr(file, ':'); + + if (sep) { + *sep = '\0'; + copy_file(ifd, file, 1); + *sep++ = ':'; + file = sep; + } else { + copy_file(ifd, file, 0); + break; + } + } +} + int main(int argc, char **argv) { int ifd = -1; @@ -647,21 +666,7 @@ int main(int argc, char **argv) file = NULL; } } - - file = params.datafile; - - for (;;) { - char *sep = strchr(file, ':'); - if (sep) { - *sep = '\0'; - copy_file (ifd, file, 1); - *sep++ = ':'; - file = sep; - } else { - copy_file (ifd, file, 0); - break; - } - } + copy_datafile(ifd, params.datafile) } else if (params.type == IH_TYPE_PBLIMAGE) { /* PBL has special Image format, implements its' own */ pbl_load_uboot(ifd, ¶ms);

On Wed, Nov 23, 2022 at 12:39:36PM +0300, Mikhail Ilin wrote:
The copy_datafile(ifd, params.datafile) function has been implemented to copy data by reducing the number of lines in the main function.
Signed-off-by: Mikhail Ilin ilin.mikhail.ol@gmail.com
Applied to u-boot/next, thanks!
participants (4)
-
Heinrich Schuchardt
-
Mikhail Ilin
-
Simon Glass
-
Tom Rini