From 8a92fae86eb261ee9349a5d148d5c9d865c91177 Mon Sep 17 00:00:00 2001 From: Steffen Trumtrar Date: Mon, 4 May 2020 10:35:41 +0200 Subject: scripts: bareboximd: fix write_file error handling write will never return 0 on POSIX conformant systems. Remove this error path. Also, close the file on error. Signed-off-by: Steffen Trumtrar Signed-off-by: Sascha Hauer --- scripts/bareboximd.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'scripts') diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c index d11b661fa3..48c3e8ab3f 100644 --- a/scripts/bareboximd.c +++ b/scripts/bareboximd.c @@ -53,7 +53,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; + int fd, ret = 0; int now; fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); @@ -62,19 +62,18 @@ 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); - return 0; + return ret; } static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size) -- cgit v1.2.3