Skip to content

Commit 2309852

Browse files
committed
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>
1 parent 55ad7e9 commit 2309852

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

loose.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ static int write_one_object(struct odb_source *source,
203203
return 0;
204204
errout:
205205
error_errno(_("failed to write loose object index %s"), path.buf);
206-
close(fd);
206+
if (fd >= 0)
207+
close(fd);
207208
rollback_lock_file(&lock);
208209
strbuf_release(&buf);
209210
strbuf_release(&path);

0 commit comments

Comments
 (0)