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:
Fabio Scotto di Santolo
2026-08-26 15:10:05 +02:00
parent 695a0f37d7
commit 9e6581986e

View File

@@ -468,12 +468,14 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
goto cleanup; goto cleanup;
hash = crc32(f); hash = crc32(f);
if (ferror(f) || fclose(f) != 0) { /* 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; f = NULL;
if (read_failed || close_failed)
goto cleanup; goto cleanup;
} }
f = NULL;
}
self->fingerprint = hash; self->fingerprint = hash;
rc = fprintf(out, "%c|%04o|%ju|%ju|%jd|%jd|%s|%ju|%s\n", self->file_type, rc = fprintf(out, "%c|%04o|%ju|%ju|%jd|%jd|%s|%ju|%s\n", self->file_type,