[PATCH 02/13] loose: avoid closing invalid fd on error path
- From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Jul 1, 2026, 07:04 UTC
- Message-ID
- <a1cd229e33c0ecf8ccbef9ab07b4b93896eae22e.1782889472.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2163.git.1782889472.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>
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> --- loose.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/loose.c b/loose.c index 47b7f5ec38..2c6db45245 100644 --- a/loose.c +++ b/loose.c @@ -202,7 +202,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);
-- gitgitgadget