diff options
author | Russell King <rmk@armlinux.org.uk> | 2019-10-31 16:30:22 +0000 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2019-11-01 21:06:42 +0100 |
commit | 1a432329b5b9dfe214e931e09cd38547936cc294 (patch) | |
tree | d29aabb2f49bd621934ceaad06b06b1668d9a1c2 | |
parent | 5d9a289f594c22361c18baf41a8a9aa5bd6a1026 (diff) | |
download | ccache-1a432329b5b9dfe214e931e09cd38547936cc294.tar.gz |
stats: Fix statistics update with full filesystem
The statistics file update does not check for write errors correctly
as stdio files are normally buffered. This means that data can be
written to the stdio buffer but not written out until fclose().
Hence, it is imperative that the fclose() return value is also
checked.
If either fprintf() or fclose() fail, clean up the temporary file,
rather than littering the filesystem with needless temporary files.
Signed-off-by: Russell King <rmk@armlinux.org.uk>
-rw-r--r-- | src/stats.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/stats.c b/src/stats.c index ff3123a2..214f3be5 100644 --- a/src/stats.c +++ b/src/stats.c @@ -340,12 +340,20 @@ stats_write(const char *path, struct counters *counters) FILE *f = create_tmp_file(&tmp_file, "wb"); for (size_t i = 0; i < counters->size; i++) { if (fprintf(f, "%u\n", counters->data[i]) < 0) { - fatal("Failed to write to %s", tmp_file); + fclose(f); + goto error; } } - fclose(f); + if (fclose(f) == EOF) { + goto error; + } x_rename(tmp_file, path); free(tmp_file); + return; + +error: + tmp_unlink(tmp_file); + fatal("Failed to write to %s", tmp_file); } static void |