mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] scripts: bareboximd: fix write_file error handling
@ 2020-05-04  8:35 Steffen Trumtrar
  2020-05-04  8:35 ` [PATCH 2/2] common: imd: handle error in imd_write_crc32 Steffen Trumtrar
  2020-05-05  5:51 ` [PATCH 1/2] scripts: bareboximd: fix write_file error handling Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Steffen Trumtrar @ 2020-05-04  8:35 UTC (permalink / raw)
  To: Barebox List

write will never return 0 on POSIX conformant systems. Remove this error
path.
Also, close the file on error.

Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
 scripts/bareboximd.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index b332d435a66f..6ba6dabf3a2c 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -56,7 +56,7 @@ int imd_command_setenv(const char *variable_name, const char *value)
 
 static int write_file(const char *filename, const void *buf, size_t size)
 {
-	int fd, ret;
+	int fd, ret = 0;
 	int now;
 
 	fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
@@ -65,22 +65,17 @@ static int write_file(const char *filename, const void *buf, size_t size)
 
 	while (size) {
 		now = write(fd, buf, size);
-		if (now == 0) {
-			errno = ENOSPC;
-			return -1;
+		if (now < 0) {
+			ret = now;
+			goto out;
 		}
-		if (now < 0)
-			return now;
 		size -= now;
 		buf += now;
 	}
 
+out:
 	close(fd);
-
-	if (ret < 0)
-		return ret;
-
-	return 0;
+	return ret;
 }
 
 static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
-- 
2.26.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] common: imd: handle error in imd_write_crc32
  2020-05-04  8:35 [PATCH 1/2] scripts: bareboximd: fix write_file error handling Steffen Trumtrar
@ 2020-05-04  8:35 ` Steffen Trumtrar
  2020-05-05  5:51 ` [PATCH 1/2] scripts: bareboximd: fix write_file error handling Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Steffen Trumtrar @ 2020-05-04  8:35 UTC (permalink / raw)
  To: Barebox List

Don't just ignore the return value of write_file.

Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
 common/imd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/common/imd.c b/common/imd.c
index 526308effa3c..5544a0131cc9 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -370,6 +370,7 @@ static int imd_write_crc32(void *buf, const struct imd_header *imd_start,
 		return -ENODATA;
 	} else {
 		uint32_t *p = (uint32_t *)(imd_crc + 1);
+		int ret;
 
 		if (*p != crc) {
 			uint32_t *flags = imd_crc32_flags(imd_crc);
@@ -377,7 +378,11 @@ static int imd_write_crc32(void *buf, const struct imd_header *imd_start,
 			debug("Update crc token from 0x%08x to 0x%08x (flags 0x%08x)\n", *p, crc, *flags);
 			*p = crc;
 
-			write_file(filename, buf, size);
+			ret = write_file(filename, buf, size);
+			if (ret < 0) {
+				eprintf("CRC: write crc token to %s failed: %d\n", filename, ret);
+				return ret;
+			}
 		}
 	}
 
-- 
2.26.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] scripts: bareboximd: fix write_file error handling
  2020-05-04  8:35 [PATCH 1/2] scripts: bareboximd: fix write_file error handling Steffen Trumtrar
  2020-05-04  8:35 ` [PATCH 2/2] common: imd: handle error in imd_write_crc32 Steffen Trumtrar
@ 2020-05-05  5:51 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2020-05-05  5:51 UTC (permalink / raw)
  To: Steffen Trumtrar; +Cc: Barebox List

On Mon, May 04, 2020 at 10:35:41AM +0200, Steffen Trumtrar wrote:
> write will never return 0 on POSIX conformant systems. Remove this error
> path.
> Also, close the file on error.
> 
> Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
> ---
>  scripts/bareboximd.c | 17 ++++++-----------
>  1 file changed, 6 insertions(+), 11 deletions(-)

Applied, thanks

Sascha


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-05-05  5:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-04  8:35 [PATCH 1/2] scripts: bareboximd: fix write_file error handling Steffen Trumtrar
2020-05-04  8:35 ` [PATCH 2/2] common: imd: handle error in imd_write_crc32 Steffen Trumtrar
2020-05-05  5:51 ` [PATCH 1/2] scripts: bareboximd: fix write_file error handling Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox