Skip to contentSkip to conversation

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.

PATCH In review Mikhail Ilin 4 participants 6 messages Nov 22, 2022 – Dec 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 tools/mkimage.c | 2 ++
 1 file changed, 2 insertions(+)
tools/mkimage.c +2 -0
index 5f51d2cc89..95005d8414 100644
@@ -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';
Signature · 1 line
2.17.1
#1 lore raw

Hi,

Quoted 6 lines · Mikhail Ilin
Fixes 0b0c6af38738 ("Prepare v2020.01")
---
 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?

Quoted 15 lines
tools/mkimage.c +2 -1
index 5f51d2cc89..95005d8414 100644
@@ -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';
--
Quoted 2 lines

2.17.1

Regards,
SImon

#2 lore raw

PATCH v2 1/1 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 d1be8f922eb3 ("mkimage: Fix variable length header support")
---
 tools/mkimage.c | 2 ++
 1 file changed, 2 insertions(+)
tools/mkimage.c +2 -0
index 5f51d2cc89..95005d8414 100644
@@ -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';
Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH v2] tools: mkimage: Fix nullptr at strchr()

Quoted 14 lines · Mikhail Ilin

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")
---
  tools/mkimage.c | 2 ++
  1 file changed, 2 insertions(+)
tools/mkimage.c +0 -0
index 5f51d2cc89..95005d8414 100644

tools/mkimage.c int main(int argc, char **argv) · line 513

Quoted 5 lines
tools/mkimage.c +2 -0
@@ -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.

tools/mkimage.c int main(int argc, char **argv) · line 514

Quoted 1 line
tools/mkimage.c +0 -0
  				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

Quoted 2 lines
tools/mkimage.c +0 -0
  				if (sep) {
  					*sep = '\0';
#4 lore raw

PATCH v3 1/1 tools: mkimage: Fix nullptr at strchr()

The copy_datafile(ifd, params.datafile) function has been implemented to copy data by reducing the number of lines in the main function.

---
 tools/mkimage.c | 35 +++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)
tools/mkimage.c +19 -0
index 30c6df7708..0b68a29285 100644
@@ -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, &params);

Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH v3] tools: mkimage: Fix nullptr at strchr()

Quoted 5 lines · Mikhail Ilin

The copy_datafile(ifd, params.datafile) function has been
implemented to copy data by reducing the number of lines in the main function.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom