From: Russell King Date: Thu, 31 Oct 2019 16:30:22 +0000 (+0000) Subject: stats: Fix statistics update with full filesystem X-Git-Tag: v3.7.6~7 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=1a432329b5b9dfe214e931e09cd38547936cc294;p=thirdparty%2Fccache.git 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 --- diff --git a/src/stats.c b/src/stats.c index ff3123a25..214f3be50 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