Subject: [PATCH 2/4] builtin/archive: close file descriptor on dup2() failure

From: Hoyoung Lee <[email-masked]>
Date: Tue, 22 Jul 2025 07:36:32 +0000

In create_output_file(), the file descriptor returned by xopen()
was not closed if dup2() failed. This leads to a potential resource
leak. Ensure the file descriptor is closed regardless of whether
dup2() succeeds or fails.

Signed-off-by: Hoyoung Lee <[email-masked]>
---
 builtin/archive.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/builtin/archive.c b/builtin/archive.c
index 13ea7308c8..c919a39f90 100644
--- a/builtin/archive.c
+++ b/builtin/archive.c
@@ -14,6 +14,7 @@ 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)
+			close(output_fd);
 			die_errno(_("could not redirect output"));
 		else
 			close(output_fd);
-- 
2.34.1