diff options
author | Krzysztof Opasiak <k.opasiak@samsung.com> | 2017-11-20 12:52:55 +0100 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2017-11-20 12:52:55 +0100 |
commit | 5837aedfccc1a94523f28a54394bdff656dfa28b (patch) | |
tree | f873b45871ddd5bf19d04182bfcef440c22d044c | |
parent | b8941d1cf5e662d6308745145e26c3b6bbc821cb (diff) | |
download | libusbg-5837aedfccc1a94523f28a54394bdff656dfa28b.tar.gz libusbg-5837aedfccc1a94523f28a54394bdff656dfa28b.tar.bz2 libusbg-5837aedfccc1a94523f28a54394bdff656dfa28b.zip |
libusbgx: fix: Check fclose() return code
According to man fclose:
"The fclose() function may also fail and set errno for any of the
errors specified for the routines close(2), write(2) or fflush(3)."
So if libc decides to cache our write till closing fd we may
miss the error if we ignore value returned from fclose().
This fixes issue #12 (github).
Reported-by: Noralf Trønnes <noralf@tronnes.org>
Suggested-by: Noralf Trønnes <noralf@tronnes.org>
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | src/usbg_common.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/usbg_common.c b/src/usbg_common.c index de9e40d..bd03498 100644 --- a/src/usbg_common.c +++ b/src/usbg_common.c @@ -164,15 +164,19 @@ int usbg_write_buf(const char *path, const char *name, goto out; } - ret = fwrite(buf, sizeof(char), len, fp); - if (ret < len) { + nmb = fwrite(buf, sizeof(char), len, fp); + if (nmb < len) { if (ferror(fp)) - ret = usbg_translate_error(errno); + nmb = usbg_translate_error(errno); else - ret = USBG_ERROR_IO; + nmb = USBG_ERROR_IO; } - fclose(fp); + ret = fclose(fp); + if (ret < 0) + ret = usbg_translate_error(errno); + else + ret = nmb; out: return ret; } @@ -192,7 +196,7 @@ int usbg_write_int(const char *path, const char *name, const char *file, if (ret > 0) ret = 0; - return 0; + return ret; } int usbg_write_string(const char *path, const char *name, @@ -204,7 +208,7 @@ int usbg_write_string(const char *path, const char *name, if (ret > 0) ret = 0; - return 0; + return ret; } int ubsg_rm_file(const char *path, const char *name) |