]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
stats: Fix statistics update with full filesystem
authorRussell King <rmk@armlinux.org.uk>
Thu, 31 Oct 2019 16:30:22 +0000 (16:30 +0000)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 1 Nov 2019 20:06:42 +0000 (21:06 +0100)
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>
src/stats.c

index ff3123a259939d3ec080e8e53ea7792688009ec7..214f3be500a72474f955dcb19a40f5c0b5f8d5f5 100644 (file)
@@ -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