From: Eric Sunshine <sunshine@sunshineco.com>
To: Hoyoung Lee <lhywkd22@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2 3/4] t/helper/test-delta: close fd if fstat() fails after open()
Date: Tue, 22 Jul 2025 04:36:14 -0400 [thread overview]
Message-ID: <CAPig+cT1hzz4_7JqesqFmgYdpsNezurcsF9XF1ep8KzvgMUc2Q@mail.gmail.com> (raw)
In-Reply-To: <20250722081219.1086866-4-lhywkd22@gmail.com>
On Tue, Jul 22, 2025 at 4:12 AM Hoyoung Lee <lhywkd22@gmail.com> wrote:
> If open() succeeds but fstat() fails, the file descriptor is not
> closed, causing a resource leak. This patch adds a close(fd) call
> in the failure path after fstat() to ensure proper resource cleanup.
>
> Signed-off-by: Hoyoung Lee <lhywkd22@gmail.com>
> ---
> diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c
> @@ -31,6 +31,7 @@ int cmd__delta(int argc, const char **argv)
> fd = open(argv[2], O_RDONLY);
> if (fd < 0 || fstat(fd, &st)) {
> perror(argv[2]);
> + close(fd);
> return 1;
> }
One condition under which this block is entered is if `fd` is less
than 0, which means close() is now being called with a negative file
descriptor, which seems quite suspect. I'd think you would want to
either restructure it into two `if` statements:
if (fd < 0) {
...
return 1;
}
if (fstat(fd, ...)) {
...
return 1;
}
or at least protect the call to close():
if (fd < 0 || fstat(fd, ...)) {
...
if (fd >= 0)
close(fd);
return 1;
}
I think the separate `if` statements are probably a bit easier to
reason about, but it is of course subjective.
next prev parent reply other threads:[~2025-07-22 8:36 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-22 8:12 [PATCH 0/4] Fix resource leaks in various helpers and builtin commands Hoyoung Lee
2025-07-22 8:12 ` [PATCH v2 1/4] t/helper/test-truncate: close file descriptor after truncation Hoyoung Lee
2025-07-22 8:12 ` [PATCH v2 2/4] builtin/archive: close file descriptor on dup2() failure Hoyoung Lee
2025-07-22 8:24 ` Jeff King
2025-07-22 8:12 ` [PATCH v2 3/4] t/helper/test-delta: close fd if fstat() fails after open() Hoyoung Lee
2025-07-22 8:36 ` Eric Sunshine [this message]
2025-07-22 8:40 ` Jeff King
2025-07-22 8:12 ` [PATCH v2 4/4] t/helper/test-delta: close fd if fstat() fails after second open() Hoyoung Lee
2025-07-22 8:25 ` Jeff King
2025-07-22 8:26 ` Jeff King
2025-07-22 14:53 ` Junio C Hamano
2025-07-22 8:35 ` [PATCH 0/4] Fix resource leaks in various helpers and builtin commands Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAPig+cT1hzz4_7JqesqFmgYdpsNezurcsF9XF1ep8KzvgMUc2Q@mail.gmail.com \
--to=sunshine@sunshineco.com \
--cc=git@vger.kernel.org \
--cc=lhywkd22@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).