diff mbox series

sloffs: Fix -Wunused-result gcc warnings in read/write

Message ID 20191108083528.103409-1-aik@ozlabs.ru
State Superseded
Headers show
Series sloffs: Fix -Wunused-result gcc warnings in read/write | expand

Commit Message

Alexey Kardashevskiy Nov. 8, 2019, 8:35 a.m. UTC
This fixes these:

sloffs.c:466:2: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
  read(fd, data, header_len);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
 tools/sloffs.c | 37 ++++++++++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 5 deletions(-)

Comments

Segher Boessenkool Nov. 8, 2019, 10:14 a.m. UTC | #1
Hi Alexey!

On Fri, Nov 08, 2019 at 07:35:28PM +1100, Alexey Kardashevskiy wrote:
> This fixes these:
> 
> sloffs.c:466:2: warning: ignoring return value of ‘read’, declared with attribute warn_unused_result [-Wunused-result]
>   read(fd, data, header_len);
>   ^~~~~~~~~~~~~~~~~~~~~~~~~~

> +	size_t rc;

> +	rc = read(fd, header, sizeof(struct stH));

read returns an ssize_t, not a size_t.  Your code should still work, but
an ssize_t here is more future-proof.

> +	if (rc != sizeof(struct stH)) {
> +		printf("Reading header, rc %ld, errno %d\n", rc, errno);

That should be %zd instead?

> +		free(header);

Should it free(sloffs->name) here as well?

> +		return NULL;
> +	}
>  	free(sloffs->name);
> +
>  	return header;
>  }

(Similar comments for the rest of the file).

HTH,


Segher
diff mbox series

Patch

diff --git a/tools/sloffs.c b/tools/sloffs.c
index 9a1eace8d619..9d0a70a796b9 100644
--- a/tools/sloffs.c
+++ b/tools/sloffs.c
@@ -22,6 +22,7 @@ 
 #include <byteswap.h>
 #include <getopt.h>
 #include <time.h>
+#include <errno.h>
 
 #include <calculatecrc.h>
 #include <crclib.h>
@@ -176,6 +177,7 @@  sloffs_header(const int fd)
 	struct sloffs file;
 	struct sloffs *sloffs;
 	struct stH *header;
+	size_t rc;
 
 	header = (struct stH *)malloc(sizeof(struct stH));
 
@@ -187,8 +189,14 @@  sloffs_header(const int fd)
 		return NULL;
 	}
 
-	read(fd, header, sizeof(struct stH));
+	rc = read(fd, header, sizeof(struct stH));
+	if (rc != sizeof(struct stH)) {
+		printf("Reading header, rc %ld, errno %d\n", rc, errno);
+		free(header);
+		return NULL;
+	}
 	free(sloffs->name);
+
 	return header;
 }
 
@@ -298,6 +306,7 @@  sloffs_append(const int file, const char *name, const char *dest)
 	struct sloffs new_file;
 	uint64_t read_len;
 	int i;
+	size_t rc;
 
 	fd = open(name, O_RDONLY);
 
@@ -328,7 +337,11 @@  sloffs_append(const int file, const char *name, const char *dest)
 
 	/* write byte at the end to be able to mmap it */
 	lseek(out, new_len - 1, SEEK_SET);
-	write(out, "", 1);
+	rc = write(out, "", 1);
+	if (rc != 1) {
+		printf("Extending file failed, rc %ld, errno %d\n", rc, errno);
+		exit(1);
+	}
 	write_start = mmap(NULL, new_len, PROT_READ | PROT_WRITE,
 			   MAP_SHARED, out, 0);
 
@@ -427,6 +440,7 @@  sloffs_dump(const int fd)
 	int i;
 	uint64_t crc;
 	uint64_t header_len;
+	size_t rc;
 
 	header = sloffs_header(fd);
 
@@ -463,7 +477,11 @@  sloffs_dump(const int fd)
 	/* no copy the header to memory to crc test it */
 	data = malloc(header_len);
 	lseek(fd, 0, SEEK_SET);
-	read(fd, data, header_len);
+	rc = read(fd, data, header_len);
+	if (rc != header_len) {
+		printf("Reading header failed, rc %ld, errno %d\n", rc, errno);
+		return;
+	}
 	crc = calCRCword((unsigned char *)data, header_length(fd), 0);
 	free(data);
 	if (!crc)
@@ -476,7 +494,11 @@  sloffs_dump(const int fd)
 	/* move to the CRC */
 	lseek(fd, crc - 8, SEEK_SET);
 	/* read it */
-	read(fd, &crc, 8);
+	rc = read(fd, &crc, 8);
+	if (rc != 8) {
+		printf("Reading crc failed, rc %ld, errno %d\n", rc, errno);
+		return;
+	}
 	crc = be64_to_cpu(crc);
 	printf("  Image CRC   : 0x%016lx CRC check: ", crc);
 	crc = check_image_crc(fd, be64_to_cpu(header->flashlen));
@@ -581,6 +603,7 @@  sloffs_copy(const int file, const char *name)
 	unsigned char *write_buf;
 	int i;
 	struct stH *header;
+	size_t rc;
 
 	header = sloffs_header(file);
 
@@ -598,7 +621,11 @@  sloffs_copy(const int file, const char *name)
 	}
 	/* write byte at the end to be able to mmap it */
 	lseek(out, len - 1, SEEK_SET);
-	write(out, "", 1);
+	rc = write(out, "", 1);
+	if (rc != 1) {
+		printf("Extending file failed, rc %ld, errno %d\n", rc, errno);
+		exit(1);
+	}
 	write_buf = mmap(NULL, len, PROT_WRITE, MAP_SHARED, out, 0);
 
 	if (write_buf == MAP_FAILED) {