From 1a432329b5b9dfe214e931e09cd38547936cc294 Mon Sep 17 00:00:00 2001 From: Russell King Date: Thu, 31 Oct 2019 16:30:22 +0000 Subject: [PATCH] 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 --- src/stats.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 -- 2.47.2