mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 03:09:51 +00:00
Close the fingerprint stream even when the read failed
RecordObjectWrite tested `ferror(f) || fclose(f) != 0`. When ferror was true the short circuit skipped fclose, and the branch then set f to NULL, disarming the cleanup that would have closed it. Every unreadable entry leaked a file descriptor inside a walk that already holds one DIR* per recursion level. Measured on 200 records whose fopen succeeds but whose read fails (a symlink to a directory): 207 descriptors held before, 7 after. Sample ferror and close unconditionally, then report either failure. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
10
src/types.c
10
src/types.c
@@ -468,11 +468,13 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
|
||||
goto cleanup;
|
||||
|
||||
hash = crc32(f);
|
||||
if (ferror(f) || fclose(f) != 0) {
|
||||
f = NULL;
|
||||
goto cleanup;
|
||||
}
|
||||
/* ferror has to be sampled before fclose, but the stream must be closed
|
||||
either way: short-circuiting on ferror would leak it. */
|
||||
const int read_failed = ferror(f);
|
||||
const int close_failed = fclose(f) != 0;
|
||||
f = NULL;
|
||||
if (read_failed || close_failed)
|
||||
goto cleanup;
|
||||
}
|
||||
self->fingerprint = hash;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user