pull: fix a "struct oid_array" memory leak

Fix a memory leak introduced in 44c175c7a46 (pull: error on no merge candidates, 2015-06-18). As a result we can mark several tests as passing with SANITIZE=leak using "TEST_PASSES_SANITIZE_LEAK=true". Removing the "int ret = 0" assignment added here in a6d7eb2c7a6 (pull: optionally rebase submodules (remote submodule changes only), 2017-06-23) is not a logic error, it could always have been left uninitialized (as "int ret"), now that we'll use the "ret" from the upper scope we can drop the assignment in the "opt_rebase" branch. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jul 1, 2022 at 12:43 UTC ece3974ba6018416ad4184c540f85d9db9b060b5
4 files changed +12 -7
builtin/pull.c
+10 -6
@@ -990,6 +990,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
990 int rebase_unspecified = 0;
991 int can_ff;
992 int divergent;
993 + int ret;
994
995 if (!getenv("GIT_REFLOG_ACTION"))
996 set_reflog_message(argc, argv);
@@ -1100,7 +1101,8 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1101 if (is_null_oid(&orig_head)) {
1102 if (merge_heads.nr > 1)
1103 die(_("Cannot merge multiple branches into empty head."));
1103 - return pull_into_void(merge_heads.oid, &curr_head);
1104 + ret = pull_into_void(merge_heads.oid, &curr_head);
1105 + goto cleanup;
1106 }
1107 if (merge_heads.nr > 1) {
1108 if (opt_rebase)
@@ -1125,8 +1127,6 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1127 }
1128
1129 if (opt_rebase) {
1128 - int ret = 0;
1129 -
1130 struct object_id newbase;
1131 struct object_id upstream;
1132 get_rebase_newbase_and_upstream(&newbase, &upstream, &curr_head,
@@ -1149,12 +1149,16 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1149 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1150 ret = rebase_submodules();
1151
1152 - return ret;
1152 + goto cleanup;
1153 } else {
1154 - int ret = run_merge();
1154 + ret = run_merge();
1155 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1156 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))
1157 ret = update_submodules();
1158 - return ret;
1158 + goto cleanup;
1159 }
1160 +
1161 +cleanup:
1162 + oid_array_clear(&merge_heads);
1163 + return ret;
1164 }
t/t5524-pull-msg.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git pull message generation'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 dollar='$Dollar'
t/t6417-merge-ours-theirs.sh
+1
@@ -4,6 +4,7 @@ test_description='Merge-recursive ours and theirs variants'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 +TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9
10 test_expect_success setup '
t/t9101-git-svn-props.sh
-1
@@ -5,7 +5,6 @@
5
6 test_description='git svn property tests'
7
8 -TEST_FAILS_SANITIZE_LEAK=true
8 . ./lib-git-svn.sh
9
10 mkdir import