Skip to contentSkip to conversation

tools: imx8image: check lseek return value

PATCH New Peng Fan 3 participants 6 messages Nov 5, 2018 – Nov 6, 2018 Arm i.MX and Layerscape

lore
Share & tools
lore

Peng Fan

author
#0 lore raw

PATCH v2 1/4 tools: imx8image: check lseek return value

Check lseek return value.

Fix Coverity CID: 184236 184235 184232

Reported-by Coverity

---

V2: Per Fabio's comments, use ret = lseek; if (ret) < 0) coding style.

 tools/imx8image.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
tools/imx8image.c +23 -4
index e6b0a146b6..03debe547e 100644
@@ -301,6 +301,7 @@ static void copy_file_aligned(int ifd, const char *datafile, int offset,
 	unsigned char *ptr;
 	uint8_t zeros[0x4000];
 	int size;
+	int ret;
 
 	if (align > 0x4000) {
 		fprintf(stderr, "Wrong alignment requested %d\n", align);
@@ -333,7 +334,13 @@ static void copy_file_aligned(int ifd, const char *datafile, int offset,
 	}
 
 	size = sbuf.st_size;
-	lseek(ifd, offset, SEEK_SET);
+	ret = lseek(ifd, offset, SEEK_SET);
+	if (ret < 0) {
+		fprintf(stderr, "%s: lseek error %s\n",
+			__func__, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+
 	if (write(ifd, ptr, size) != size) {
 		fprintf(stderr, "Write error %s\n", strerror(errno));
 		exit(EXIT_FAILURE);
@@ -359,7 +366,7 @@ static void copy_file (int ifd, const char *datafile, int pad, int offset)
 	int tail;
 	int zero = 0;
 	uint8_t zeros[4096];
-	int size;
+	int size, ret;
 
 	memset(zeros, 0, sizeof(zeros));
 
@@ -387,7 +394,13 @@ static void copy_file (int ifd, const char *datafile, int pad, int offset)
 	}
 
 	size = sbuf.st_size;
-	lseek(ifd, offset, SEEK_SET);
+	ret = lseek(ifd, offset, SEEK_SET);
+	if (ret < 0) {
+		fprintf(stderr, "%s: lseek error %s\n",
+			__func__, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
+
 	if (write(ifd, ptr, size) != size) {
 		fprintf(stderr, "Write error %s\n",
 			strerror(errno));
@@ -762,6 +775,7 @@ static int build_container(soc_type_t soc, uint32_t sector_size,
 	char *tmp_filename = NULL;
 	uint32_t size = 0;
 	uint32_t file_padding = 0;
+	int ret;
 
 	int container = -1;
 	int cont_img_count = 0; /* indexes to arrange the container */
@@ -883,7 +897,12 @@ static int build_container(soc_type_t soc, uint32_t sector_size,
 	} while (img_sp->option != NO_IMG);
 
 	/* Add padding or skip appended container */
-	lseek(ofd, file_padding, SEEK_SET);
+	ret = lseek(ofd, file_padding, SEEK_SET);
+	if (ret < 0) {
+		fprintf(stderr, "%s: lseek error %s\n",
+			__func__, strerror(errno));
+		exit(EXIT_FAILURE);
+	}
 
 	/* Note: Image offset are not contained in the image */
 	tmp = flatten_container_header(&imx_header, container + 1, &size,
Signature · 1 line
2.14.1
author
#1 lore raw

PATCH v2 2/4 tools: imx8image: fix coverity CID 184234

Fix:
CID 184234: (TAINTED_SCALAR)
Using tainted variable "header.num_images - 1" as an index into an array "header.img".

Reported-by Coverity
---
 tools/imx8image.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/imx8image.c +3 -1
index 03debe547e..019b875773 100644
@@ -666,8 +666,10 @@ static int get_container_image_start_pos(image_t *image_stack, uint32_t align)
 			}
 
 			ret = fread(&header, sizeof(header), 1, fd);
-			if (ret != 1)
+			if (ret != 1) {
 				printf("Failure Read header %d\n", ret);
+				exit(EXIT_FAILURE);
+			}
 
 			fclose(fd);
 
Signature · 1 line
2.14.1
author
#2 lore raw

PATCH v2 3/4 tools: imx8image: fix coverity CID 184233

In reply to Peng Fan

Fix:
CID 184233: (NEGATIVE_RETURNS)
Using variable "container" as an index to array "imx_header.fhdr".

Reported-by Coverity
---
 tools/imx8image.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/imx8image.c +8 -0
index 019b875773..ac89c821c9 100644
@@ -812,6 +812,10 @@ static int build_container(soc_type_t soc, uint32_t sector_size,
 		case SCFW:
 		case DATA:
 		case MSG_BLOCK:
+			if (container < 0) {
+				fprintf(stderr, "No container found\n");
+				exit(EXIT_FAILURE);
+			}
 			check_file(&sbuf, img_sp->filename);
 			tmp_filename = img_sp->filename;
 			set_image_array_entry(&imx_header.fhdr[container],
@@ -825,6 +829,10 @@ static int build_container(soc_type_t soc, uint32_t sector_size,
 			break;
 
 		case SECO:
+			if (container < 0) {
+				fprintf(stderr, "No container found\n");
+				exit(EXIT_FAILURE);
+			}
 			check_file(&sbuf, img_sp->filename);
 			tmp_filename = img_sp->filename;
 			set_image_array_entry(&imx_header.fhdr[container],
Signature · 1 line
2.14.1
author
#3 lore raw

PATCH v2 4/4 tools: imx8image: flatten container header only when creating container

In reply to Peng Fan

If there is no CONTAINER entry, there is no need to flatten container header.

---
 tools/imx8image.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
tools/imx8image.c +12 -10
index ac89c821c9..6e8ac464e7 100644
@@ -914,17 +914,19 @@ static int build_container(soc_type_t soc, uint32_t sector_size,
 		exit(EXIT_FAILURE);
 	}
 
-	/* Note: Image offset are not contained in the image */
-	tmp = flatten_container_header(&imx_header, container + 1, &size,
-				       file_padding);
-	/* Write image header */
-	if (write(ofd, tmp, size) != size) {
-		fprintf(stderr, "error writing image hdr\n");
-		exit(EXIT_FAILURE);
-	}
+	if (container >= 0) {
+		/* Note: Image offset are not contained in the image */
+		tmp = flatten_container_header(&imx_header, container + 1,
+					       &size, file_padding);
+		/* Write image header */
+		if (write(ofd, tmp, size) != size) {
+			fprintf(stderr, "error writing image hdr\n");
+			exit(EXIT_FAILURE);
+		}
 
-	/* Clean-up memory used by the headers */
-	free(tmp);
+		/* Clean-up memory used by the headers */
+		free(tmp);
+	}
 
 	/*
 	 * step through the image stack again this time copying
Signature · 1 line
2.14.1
#5 lore raw

PATCH v2 1/4 tools: imx8image: check lseek return value

Hi Peng,

Quoted 6 lines · Fabio Estevam

Hi Peng,

Quoted 3 lines · Peng Fan

Check lseek return value.

Quoted 9 lines
Quoted 4 lines

Fix Coverity CID: 184236 184235 184232

Reported-by Coverity

Thanks for the respin.

Yes, thanks for this. I admit that I prefer in this way as Fabio, but I admit that it is not thinkable that lseek() will change the API, and I agree that change could be more or less a question of taste. Anyway, I prefer in this way...

I merged the series together with:

- MAINTAINERS: add NXP linux team maillist as i.MX reviewer
- ARM: dts: fsl-imx8qxp-mek: Move regulator outside "simple-bus"
- mx8mq_evk: README: Delete file introduced by mistake

I do not see any other open points and I will send PR to Tom.

Best regards,
Stefano

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: sbabic at denx.de
=====================================================================