merge: free result of repo_get_merge_bases()
We call repo_get_merge_bases(), which allocates a commit_list, but never free the result, causing a leak. The obvious solution is to free it, but we need to look at the contents of the first item to decide whether to leave the loop. One option is to free it in both code paths. But since the commit that the list points to is longer-lived than the list itself, we can just dereference it immediately, free the list, and then continue with the existing logic. This is about the same amount of code, but keeps the list management all in one place. This lets us mark a number of merge-related test scripts as leak-free. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Oct 3, 2023 at 16:27 UTC
716a6b2c3a54779e1e6f115a5950511d019f4d17
11 files changed
+14
-1
builtin/merge.c
+4
-1
@@ -1634,6 +1634,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1634
1635
for (j = remoteheads; j; j = j->next) {
1636
struct commit_list *common_one;
1637
+ struct commit *common_item;
1638
1639
/*
1640
* Here we *have* to calculate the individual
@@ -1643,7 +1644,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1644
common_one = repo_get_merge_bases(the_repository,
1645
head_commit,
1646
j->item);
1646
- if (!oideq(&common_one->item->object.oid, &j->item->object.oid)) {
1647
+ common_item = common_one->item;
1648
+ free_commit_list(common_one);
1649
+ if (!oideq(&common_item->object.oid, &j->item->object.oid)) {
1650
up_to_date = 0;
1651
break;
1652
}
t/t4214-log-graph-octopus.sh
+1
@@ -5,6 +5,7 @@ test_description='git log --graph of skewed left octopus merge.'
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
. "$TEST_DIRECTORY"/lib-log-graph.sh
11
t/t4215-log-skewed-merges.sh
+1
@@ -2,6 +2,7 @@
2
3
test_description='git log --graph of skewed merges'
4
5
+TEST_PASSES_SANITIZE_LEAK=true
6
. ./test-lib.sh
7
. "$TEST_DIRECTORY"/lib-log-graph.sh
8
t/t6009-rev-list-parent.sh
+1
@@ -5,6 +5,7 @@ test_description='ancestor culling and limiting by parent number'
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
11
check_revlist () {
t/t6416-recursive-corner-cases.sh
+1
@@ -5,6 +5,7 @@ test_description='recursive merge corner cases involving criss-cross merges'
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
. "$TEST_DIRECTORY"/lib-merge.sh
11
t/t6433-merge-toplevel.sh
+1
@@ -5,6 +5,7 @@ test_description='"git merge" top-level frontend'
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8
+TEST_PASSES_SANITIZE_LEAK=true
9
. ./test-lib.sh
10
11
t3033_reset () {
t/t6437-submodule-merge.sh
+1
@@ -8,6 +8,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8
GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
9
export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
10
11
+TEST_PASSES_SANITIZE_LEAK=true
12
. ./test-lib.sh
13
. "$TEST_DIRECTORY"/lib-merge.sh
14
t/t7602-merge-octopus-many.sh
+1
@@ -4,6 +4,7 @@ test_description='git merge
4
5
Testing octopus merge with more than 25 refs.'
6
7
+TEST_PASSES_SANITIZE_LEAK=true
8
. ./test-lib.sh
9
10
test_expect_success 'setup' '
t/t7603-merge-reduce-heads.sh
+1
@@ -4,6 +4,7 @@ test_description='git merge
4
5
Testing octopus merge when reducing parents to independent branches.'
6
7
+TEST_PASSES_SANITIZE_LEAK=true
8
. ./test-lib.sh
9
10
# 0 - 1
t/t7607-merge-state.sh
+1
@@ -4,6 +4,7 @@ test_description="Test that merge state is as expected after failed merge"
4
5
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
+TEST_PASSES_SANITIZE_LEAK=true
8
. ./test-lib.sh
9
10
test_expect_success 'Ensure we restore original state if no merge strategy handles it' '
t/t7608-merge-messages.sh
+1
@@ -4,6 +4,7 @@ test_description='test auto-generated merge messages'
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
check_oneline() {