git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hoyoung Lee <lhywkd22@gmail.com>
To: git@vger.kernel.org
Cc: Hoyoung Lee <lhywkd22@gmail.com>
Subject: [PATCH v3 2/2] t/helper/test-delta: fix possible resource leak and ensure safe cleanup
Date: Tue, 22 Jul 2025 17:41:02 +0000	[thread overview]
Message-ID: <20250722174102.1876197-3-lhywkd22@gmail.com> (raw)
In-Reply-To: <20250722174102.1876197-1-lhywkd22@gmail.com>

Initialize `fd` to -1 and unify all `open()`-related `close()` calls
under a single cleanup label. This prevents undefined behavior when
`fd` is used without initialization in error paths.

The cleanup logic now safely avoids calling `close()` on invalid
descriptors and ensures consistent resource management.

Signed-off-by: Hoyoung Lee <lhywkd22@gmail.com>
---
 t/helper/test-delta.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c
index 6bc787a474..f5811e96ad 100644
--- a/t/helper/test-delta.c
+++ b/t/helper/test-delta.c
@@ -17,7 +17,7 @@ static const char usage_str[] =
 
 int cmd__delta(int argc, const char **argv)
 {
-	int fd;
+	int fd = -1;
 	struct stat st;
 	void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL;
 	unsigned long from_size, data_size, out_size;
@@ -31,13 +31,12 @@ int cmd__delta(int argc, const char **argv)
 	fd = open(argv[2], O_RDONLY);
 	if (fd < 0 || fstat(fd, &st)) {
 		perror(argv[2]);
-		return 1;
+		goto cleanup;
 	}
 	from_size = st.st_size;
 	from_buf = xmalloc(from_size);
 	if (read_in_full(fd, from_buf, from_size) < 0) {
 		perror(argv[2]);
-		close(fd);
 		goto cleanup;
 	}
 	close(fd);
@@ -51,7 +50,6 @@ int cmd__delta(int argc, const char **argv)
 	data_buf = xmalloc(data_size);
 	if (read_in_full(fd, data_buf, data_size) < 0) {
 		perror(argv[3]);
-		close(fd);
 		goto cleanup;
 	}
 	close(fd);
@@ -81,5 +79,8 @@ int cmd__delta(int argc, const char **argv)
 	free(data_buf);
 	free(out_buf);
 
+	if (fd >= 0)
+		close(fd);
+
 	return ret;
 }
-- 
2.34.1


  parent reply	other threads:[~2025-07-22 17:41 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-22 17:41 [PATCH v3 0/2] fix resource leaks in test helpers Hoyoung Lee
2025-07-22 17:41 ` [PATCH v3 1/2] t/helper/test-truncate: close file descriptor after truncation Hoyoung Lee
2025-07-22 21:48   ` Junio C Hamano
2025-07-22 17:41 ` Hoyoung Lee [this message]
2025-07-23  7:28   ` [PATCH v3 2/2] t/helper/test-delta: fix possible resource leak and ensure safe cleanup Eric Sunshine
2025-07-23  7:55     ` Jeff King
2025-07-23  8:06       ` Jeff King
2025-07-23  8:17         ` Eric Sunshine
2025-07-23 23:59         ` Jeff King
2025-07-24  0:00           ` [PATCH 1/3] test-delta: handle errors with die() Jeff King
2025-07-24  0:02           ` [PATCH 2/3] test-delta: use strbufs to hold input files Jeff King
2025-07-24  0:03           ` [PATCH 3/3] test-delta: close output descriptor after use Jeff King
2025-07-23  8:11       ` [PATCH v3 2/2] t/helper/test-delta: fix possible resource leak and ensure safe cleanup Eric Sunshine
2025-07-23  8:46         ` Jeff King
2025-07-23 16:37           ` Eric Sunshine
2025-07-23 17:11         ` Junio C Hamano
2025-07-23 16:48       ` Junio C Hamano
2025-07-23 16:44     ` Junio C Hamano

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=20250722174102.1876197-3-lhywkd22@gmail.com \
    --to=lhywkd22@gmail.com \
    --cc=git@vger.kernel.org \
    /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).