loose: avoid closing invalid fd on error path

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

Johannes Schindelin committed Jul 5, 2026 at 08:24 UTC bd58327406c6868b92fb1b61d85b7430839042d4
1 file changed +2 -1
loose.c
+2 -1
@@ -201,7 +201,8 @@ static int write_one_object(struct odb_source_loose *loose,
201 return 0;
202 errout:
203 error_errno(_("failed to write loose object index %s"), path.buf);
204 - close(fd);
204 + if (fd >= 0)
205 + close(fd);
206 rollback_lock_file(&lock);
207 strbuf_release(&buf);
208 strbuf_release(&path);