builtin/submodule--helper: fix leak when remote_submodule_branch() failed

In builtin/submodule--helper.c:update_submodule(), the variable remote_name is allocated in get_default_remote_submodule() but may be leaked if remote_submodule_branch() fails. Although it is unlikely that remote_submodule_branch() would fail after successfully obtaining a remote ref name from get_default_remote_submodule(), it is still possible. To prevent a potential memory leak, add a call to free(remote_name) at the early exit point. Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lidong Yan committed Jun 8, 2025 at 03:56 UTC bfc9f9cc6454609e63fefdb95e3dc4f25fcdc8ef
1 file changed +3 -1
builtin/submodule--helper.c
+3 -1
@@ -2660,8 +2660,10 @@ static int update_submodule(struct update_data *update_data)
2660 if (code)
2661 return code;
2662 code = remote_submodule_branch(update_data->sm_path, &branch);
2663 - if (code)
2663 + if (code) {
2664 + free(remote_name);
2665 return code;
2666 + }
2667 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2668
2669 free(remote_name);