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>
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