{"thread":{"id":"63602","subject":"[PATCH] builtin/submodule--helper: fix leak when remote_submodule_branch() failed","startedAt":"2025-06-08T03:57:03Z","lastAt":"2025-06-08T03:57:03Z","messageCount":1,"participants":["Lidong Yan via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"519907","messageId":"pull.1987.git.git.1749355019495.gitgitgadget@gmail.com","threadId":"63602","inReplyTo":null,"subject":"[PATCH] builtin/submodule--helper: fix leak when remote_submodule_branch() failed","fromName":"Lidong Yan via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-06-08T03:56:59Z","receivedAt":"2025-06-08T03:57:03Z","isPatch":true,"sender":{"key":"yldhome2d2@gmail.com","avatar":"https://avatars.githubusercontent.com/u/77328395?v=4"},"body":"From: Lidong Yan <502024330056@smail.nju.edu.cn>\n\nIn builtin/submodule--helper.c:update_submodule(), the variable\nremote_name is allocated in get_default_remote_submodule() but\nmay be leaked if remote_submodule_branch() fails. Although it is\nunlikely that remote_submodule_branch() would fail after successfully\nobtaining a remote ref name from get_default_remote_submodule(),\nit is still possible. To prevent a potential memory leak, add a\ncall to free(remote_name) at the early exit point.\n\nSigned-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>\n---\n    submodule--helper: fix leak when remote_submodule_branch() failed\n    \n    In builtin/submodule--helper.c:update_submodule(), remote_name is\n    allocated in get_default_remote_submodule() but may leak due to failure\n    of remote_submodule_branch. Add free(remote_name) at the eraly exit\n    point.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1987%2Fbrandb97%2Ffix-update-submodule-leak-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1987/brandb97/fix-update-submodule-leak-v1\nPull-Request: https://github.com/git/git/pull/1987\n\n builtin/submodule--helper.c | 4 +++-\n 1 file changed, 3 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 53da2116ddf..640c0059c3e 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -2660,8 +2660,10 @@ static int update_submodule(struct update_data *update_data)\n \t\tif (code)\n \t\t\treturn code;\n \t\tcode = remote_submodule_branch(update_data->sm_path, &branch);\n-\t\tif (code)\n+\t\tif (code) {\n+\t\t\tfree(remote_name);\n \t\t\treturn code;\n+\t\t}\n \t\tremote_ref = xstrfmt(\"refs/remotes/%s/%s\", remote_name, branch);\n \n \t\tfree(remote_name);\n\nbase-commit: 7014b55638da979331baf8dc31c4e1d697cf2d67\n-- \ngitgitgadget\n"}]}