git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/4] t/helper/test-delta: close fd if fstat() fails after second open()

From
Hoyoung Lee <lhywkd22@gmail.com>
Date
Jul 22, 2025, 08:12 UTC
Message-ID
<20250722081219.1086866-5-lhywkd22@gmail.com>
In-Reply-To
<20250722081219.1086866-1-lhywkd22@gmail.com>

When opening argv[3], if open() succeeds but fstat() fails, the file descriptor is not closed, resulting in a resource leak. This patch ensures that the descriptor is closed on failure.

Signed-off-by: Hoyoung Lee <lhywkd22@gmail.com>
---
 builtin/archive.c     | 3 ++-
 t/helper/test-delta.c | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/builtin/archive.c b/builtin/archive.c
index c919a39f90..951fc2e444 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -13,9 +13,10 @@ static void create_output_file(const char *output_file)
 {
 	int output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
 	if (output_fd != 1) {
-		if (dup2(output_fd, 1) < 0)
+		if (dup2(output_fd, 1) < 0) {
 			close(output_fd);
 			die_errno(_("could not redirect output"));
+		}
 		else
 			close(output_fd);
 	}
diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c
index 103bf7f3e9..ba2d897aa3 100644
--- a/t/helper/test-delta.c
+++ b/t/helper/test-delta.c
@@ -46,6 +46,8 @@ int cmd__delta(int argc, const char **argv)
 	fd = open(argv[3], O_RDONLY);
 	if (fd < 0 || fstat(fd, &st)) {
 		perror(argv[3]);
+		if (fd >= 0)
+			close(fd);
 		goto cleanup;
 	}
 	data_size = st.st_size;
-- 
2.34.1
Previous: Jeff KingNext: Jeff King
Message 8 of 12 in “Fix resource leaks in various helpers and builtin commands”
  1. 0/4 Fix resource leaks in various helpers and builtin commandsHoyoung Lee, Jul 22, 2025
  2. 1/4 t/helper/test-truncate: close file descriptor after truncationHoyoung Lee, Jul 22, 2025
  3. 2/4 builtin/archive: close file descriptor on dup2() failureHoyoung Lee, Jul 22, 2025
  4. Jeff KingJul 22, 2025
  5. 3/4 t/helper/test-delta: close fd if fstat() fails after open()Hoyoung Lee, Jul 22, 2025
  6. Eric SunshineJul 22, 2025
  7. Jeff KingJul 22, 2025
  8. 4/4 t/helper/test-delta: close fd if fstat() fails after second open()Hoyoung Lee, Jul 22, 2025
  9. Jeff KingJul 22, 2025
  10. Jeff KingJul 22, 2025
  11. Junio C HamanoJul 22, 2025
  12. Jeff KingJul 22, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.