{"thread":{"id":"63816","subject":"[PATCH 0/4] Fix resource leaks in various helpers and builtin commands","startedAt":"2025-07-22T07:37:47Z","lastAt":"2025-07-22T07:38:36Z","messageCount":5,"participants":["Hoyoung Lee"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"522395","messageId":"20250722073634.1080217-1-lhywkd22@gmail.com","threadId":"63816","inReplyTo":null,"subject":"[PATCH 0/4] Fix resource leaks in various helpers and builtin commands","fromName":"Hoyoung Lee","fromEmail":"lhywkd22@gmail.com","sentAt":"2025-07-22T07:36:30Z","receivedAt":"2025-07-22T07:37:47Z","isPatch":true,"sender":{"key":"lhywkd22@gmail.com","avatar":"https://avatars.githubusercontent.com/u/204378075?v=4"},"body":"This patch series fixes several cases where file descriptors were not\nproperly closed on error paths. The changes affect helper programs and\na builtin command, and ensure that system resources are correctly\nreleased before returning from the function.\n\nEach fix is minimal and follows the existing style of the surrounding\ncode. These changes help improve the robustness of the code by avoiding\npotential file descriptor leaks.\n\nHoyoung Lee (4):\n  t/helper/test-truncate: close file descriptor after truncation\n  builtin/archive: close file descriptor on dup2() failure\n  t/helper/test-delta: close fd if fstat() fails after open()\n  t/helper/test-delta: close fd if fstat() fails after second open()\n\n builtin/archive.c        | 1 +\n t/helper/test-delta.c    | 3 +++\n t/helper/test-truncate.c | 3 +++\n 3 files changed, 7 insertions(+)\n\n-- \n2.34.1\n\n"},{"id":"522396","messageId":"20250722073634.1080217-2-lhywkd22@gmail.com","threadId":"63816","inReplyTo":"20250722073634.1080217-1-lhywkd22@gmail.com","subject":"[PATCH 1/4] t/helper/test-truncate: close file descriptor after truncation","fromName":"Hoyoung Lee","fromEmail":"lhywkd22@gmail.com","sentAt":"2025-07-22T07:36:31Z","receivedAt":"2025-07-22T07:38:32Z","isPatch":true,"sender":{"key":"lhywkd22@gmail.com","avatar":"https://avatars.githubusercontent.com/u/204378075?v=4"},"body":"Fix a resource leak where the file descriptor was not closed after\ntruncating a file in t/helper/test-truncate.c.\n\nSigned-off-by: Hoyoung Lee <lhywkd22@gmail.com>\n---\n t/helper/test-truncate.c | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/t/helper/test-truncate.c b/t/helper/test-truncate.c\nindex 3931deaec7..104bc36cc0 100644\n--- a/t/helper/test-truncate.c\n+++ b/t/helper/test-truncate.c\n@@ -21,5 +21,8 @@ int cmd__truncate(int argc, const char **argv)\n \n \tif (ftruncate(fd, (off_t) sz) < 0)\n \t\tdie_errno(\"failed to truncate file\");\n+\t\n+\tclose(fd);\n+\n \treturn 0;\n }\n-- \n2.34.1\n\n"},{"id":"522397","messageId":"20250722073634.1080217-3-lhywkd22@gmail.com","threadId":"63816","inReplyTo":"20250722073634.1080217-1-lhywkd22@gmail.com","subject":"[PATCH 2/4] builtin/archive: close file descriptor on dup2() failure","fromName":"Hoyoung Lee","fromEmail":"lhywkd22@gmail.com","sentAt":"2025-07-22T07:36:32Z","receivedAt":"2025-07-22T07:38:33Z","isPatch":true,"sender":{"key":"lhywkd22@gmail.com","avatar":"https://avatars.githubusercontent.com/u/204378075?v=4"},"body":"In create_output_file(), the file descriptor returned by xopen()\nwas not closed if dup2() failed. This leads to a potential resource\nleak. Ensure the file descriptor is closed regardless of whether\ndup2() succeeds or fails.\n\nSigned-off-by: Hoyoung Lee <lhywkd22@gmail.com>\n---\n builtin/archive.c | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/builtin/archive.c b/builtin/archive.c\nindex 13ea7308c8..c919a39f90 100644\n--- a/builtin/archive.c\n+++ b/builtin/archive.c\n@@ -14,6 +14,7 @@ static void create_output_file(const char *output_file)\n \tint output_fd = xopen(output_file, O_CREAT | O_WRONLY | O_TRUNC, 0666);\n \tif (output_fd != 1) {\n \t\tif (dup2(output_fd, 1) < 0)\n+\t\t\tclose(output_fd);\n \t\t\tdie_errno(_(\"could not redirect output\"));\n \t\telse\n \t\t\tclose(output_fd);\n-- \n2.34.1\n\n"},{"id":"522398","messageId":"20250722073634.1080217-4-lhywkd22@gmail.com","threadId":"63816","inReplyTo":"20250722073634.1080217-1-lhywkd22@gmail.com","subject":"[PATCH 3/4] t/helper/test-delta: close fd if fstat() fails after open()","fromName":"Hoyoung Lee","fromEmail":"lhywkd22@gmail.com","sentAt":"2025-07-22T07:36:33Z","receivedAt":"2025-07-22T07:38:35Z","isPatch":true,"sender":{"key":"lhywkd22@gmail.com","avatar":"https://avatars.githubusercontent.com/u/204378075?v=4"},"body":"If open() succeeds but fstat() fails, the file descriptor is not\nclosed, causing a resource leak. This patch adds a close(fd) call\nin the failure path after fstat() to ensure proper resource cleanup.\n\nSigned-off-by: Hoyoung Lee <lhywkd22@gmail.com>\n---\n t/helper/test-delta.c | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/t/helper/test-delta.c b/t/helper/test-delta.c\nindex 6bc787a474..103bf7f3e9 100644\n--- a/t/helper/test-delta.c\n+++ b/t/helper/test-delta.c\n@@ -31,6 +31,7 @@ int cmd__delta(int argc, const char **argv)\n \tfd = open(argv[2], O_RDONLY);\n \tif (fd < 0 || fstat(fd, &st)) {\n \t\tperror(argv[2]);\n+\t\tclose(fd);\n \t\treturn 1;\n \t}\n \tfrom_size = st.st_size;\n-- \n2.34.1\n\n"},{"id":"522399","messageId":"20250722073634.1080217-5-lhywkd22@gmail.com","threadId":"63816","inReplyTo":"20250722073634.1080217-1-lhywkd22@gmail.com","subject":"[PATCH 4/4] t/helper/test-delta: close fd if fstat() fails after second open()","fromName":"Hoyoung Lee","fromEmail":"lhywkd22@gmail.com","sentAt":"2025-07-22T07:36:34Z","receivedAt":"2025-07-22T07:38:36Z","isPatch":true,"sender":{"key":"lhywkd22@gmail.com","avatar":"https://avatars.githubusercontent.com/u/204378075?v=4"},"body":"When opening argv[3], if open() succeeds but fstat() fails,\nthe file descriptor is not closed, resulting in a resource leak.\nThis patch ensures that the descriptor is closed on failure.\n\nSigned-off-by: Hoyoung Lee <lhywkd22@gmail.com>\n---\n t/helper/test-delta.c | 2 ++\n 1 file changed, 2 insertions(+)\n\ndiff --git a/t/helper/test-delta.c b/t/helper/test-delta.c\nindex 103bf7f3e9..ba2d897aa3 100644\n--- a/t/helper/test-delta.c\n+++ b/t/helper/test-delta.c\n@@ -46,6 +46,8 @@ int cmd__delta(int argc, const char **argv)\n \tfd = open(argv[3], O_RDONLY);\n \tif (fd < 0 || fstat(fd, &st)) {\n \t\tperror(argv[3]);\n+\t\tif (fd >= 0)\n+\t\t\tclose(fd);\n \t\tgoto cleanup;\n \t}\n \tdata_size = st.st_size;\n-- \n2.34.1\n\n"}]}