Re: [PATCH 02/13] loose: avoid closing invalid fd on error path
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Jul 1, 2026, 07:56 UTC
- Message-ID
- <akTIIaxwqqFaVxj1@pks.im>
- In-Reply-To
- <a1cd229e33c0ecf8ccbef9ab07b4b93896eae22e.1782889472.git.gitgitgadget@gmail.com>
On Wed, Jul 01, 2026 at 07:04:20AM +0000, Johannes Schindelin via GitGitGadget wrote:
Show 14 quoted lines
> 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);Makes sense. At the time we hit the first `goto errout` we have already assigned `fd = open(...)`, so we know it should be either negative or a positive file descriptor.
There's also a second call to `close(fd)`, but if that call is successful then we would not use the `errout` path. If it fails we may try to close the file descriptor a second time, but that's probably a non-issue.
Patrick