]> git.ipfire.org Git - thirdparty/git.git/commitdiff
loose: avoid closing invalid fd on error path
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Sun, 5 Jul 2026 08:24:19 +0000 (08:24 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 5 Jul 2026 16:12:09 +0000 (09:12 -0700)
`write_one_object()` opens a file at line 186 and jumps to the errout
label on failure. The errout cleanup unconditionally calls `close(fd)`,
but when `open()` itself failed, fd is -1. Calling `close(-1)` is
harmless on most platforms (returns EBADF) but is undefined behavior per
POSIX and can confuse fd tracking in sanitizer builds.

Guard the close with fd >= 0.

Pointed out by Coverity.

Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
loose.c

diff --git a/loose.c b/loose.c
index 940a9e0dfee879dd66ee54ef5abcd282c0a76d9a..bf01d3e42def345d199cef45512c14beefbcf1ee 100644 (file)
--- a/loose.c
+++ b/loose.c
@@ -201,7 +201,8 @@ static int write_one_object(struct odb_source_loose *loose,
        return 0;
 errout:
        error_errno(_("failed to write loose object index %s"), path.buf);
-       close(fd);
+       if (fd >= 0)
+               close(fd);
        rollback_lock_file(&lock);
        strbuf_release(&buf);
        strbuf_release(&path);