From 9e6581986ed88fe7988cfc0c828c0f67bc414d63 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 15:10:05 +0200 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- src/types.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/types.c b/src/types.c index 5f41440..c69cb20 100644 --- a/src/types.c +++ b/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;