Skip to contentSkip to conversation

This fixes a minor memory leak (detected by LeakSanitizer) in git merge.

PATCH Superseded Kevin Backhouse via GitGitGadget 3 participants 8 messages Aug 18, 2023 – Sep 12, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 This fixes a minor memory leak (detected by LeakSanitizer) in git merge.

From: Kevin Backhouse <[email protected]>

To reproduce (with an ASAN build):

```
mkdir test
cd test
git init
echo x > x.txt
git add .
git commit -m "WIP"
git checkout -b dev
echo y > x.txt
git add .
git commit -m "WIP"
git checkout main
echo z > x.txt
git add .
git commit -m "WIP"
echo a > x.txt
git add .
git merge dev
```

The fix is to call free_commit_list(merge_bases) when an error occurs.

---
This fixes a minor memory leak (detected by LeakSanitizer) in git merge

To reproduce (with an ASAN build):

mkdir test
cd test
git init
echo x > x.txt
git add .
git commit -m "WIP"
git checkout -b dev
echo y > x.txt
git add .
git commit -m "WIP"
git checkout main
echo z > x.txt
git add .
git commit -m "WIP"
echo a > x.txt
git add .
git merge dev

The fix is to call free_commit_list(merge_bases) when an error occurs.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1577%2Fkevinbackhouse%2Ffree-merge-bases-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1577/kevinbackhouse/free-merge-bases-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1577

 merge-ort-wrappers.c | 4 +++-
 merge-ort.c          | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
merge-ort-wrappers.c +3 -1
index 4acedf3c338..aeb56c9970c 100644
@@ -54,8 +54,10 @@ int merge_ort_recursive(struct merge_options *opt,
 	struct tree *head = repo_get_commit_tree(opt->repo, side1);
 	struct merge_result tmp;
 
-	if (unclean(opt, head))
+	if (unclean(opt, head)) {
+		free_commit_list(merge_bases);
 		return -1;
+	}
 
 	memset(&tmp, 0, sizeof(tmp));
 	merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);
merge-ort.c +3 -1
index 8631c997002..a0eb91fb011 100644
@@ -5070,8 +5070,10 @@ static void merge_ort_internal(struct merge_options *opt,
 		opt->branch1 = "Temporary merge branch 1";
 		opt->branch2 = "Temporary merge branch 2";
 		merge_ort_internal(opt, NULL, prev, next, result);
-		if (result->clean < 0)
+		if (result->clean < 0) {
+			free_commit_list(merge_bases);
 			return;
+		}
 		opt->branch1 = saved_b1;
 		opt->branch2 = saved_b2;
 		opt->priv->call_depth--;

base-commit: f9972720e9a405e4f6924a7cde0ed5880687f4d0

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 15 lines · Kevin Backhouse via GitGitGadget

From: Kevin Backhouse <[email protected]>

To reproduce (with an ASAN build):

```
mkdir test
cd test
git init
echo x > x.txt
git add .
git commit -m "WIP"
git checkout -b dev
echo y > x.txt
git add .
git commit -m "WIP"
Quoted 8 lines
git checkout main
echo z > x.txt
git add .
git commit -m "WIP"
echo a > x.txt
git add .
git merge dev
```

We'd rather not to see the above in the proposed log message; can't we add (a variation of) it to our test suite?

Quoted 1 line

The fix is to call free_commit_list(merge_bases) when an error occurs.

We usually have the description of what the problem is and give an analysis on why/how it happens, before presenting a solution. Write it more like:

The caller of merge_ort_recursive() expects the commit list passed in as the merge_bases parameter to be fully consumed by the function and does not free it when the function returns. In normal cases, the commit list does get consumed, but when the function returns early upon encountering an error, it forgets to clean it up.

Fix this by freeing the list in the code paths for error returns.

Quoted 2 lines
 merge-ort-wrappers.c | 4 +++-
 merge-ort.c          | 4 +++-

These two places and their fixes seem OK, but I have to wonder if these are complete fixes.

Quoted 6 lines
merge-ort-wrappers.c +0 -0
index 4acedf3c338..aeb56c9970c 100644
@@ -54,8 +54,10 @@ int merge_ort_recursive(struct merge_options *opt,
 	struct tree *head = repo_get_commit_tree(opt->repo, side1);

merge-ort-wrappers.c int merge_ort_recursive(struct merge_options *opt, · line 63

Quoted 10 lines
merge-ort-wrappers.c +3 -1
 	struct merge_result tmp;
 
-	if (unclean(opt, head))
+	if (unclean(opt, head)) {
+		free_commit_list(merge_bases);
 		return -1;
+	}
 
 	memset(&tmp, 0, sizeof(tmp));
 	merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);

The function before this hunk appears to have very similar code structure. Does it need the same fix, or if not why not?

merge-ort.c static void merge_ort_internal(struct merge_options *opt, · line 5076

Quoted 13 lines
merge-ort.c +3 -1
index 8631c997002..a0eb91fb011 100644
@@ -5070,8 +5070,10 @@ static void merge_ort_internal(struct merge_options *opt,
 		opt->branch1 = "Temporary merge branch 1";
 		opt->branch2 = "Temporary merge branch 2";
 		merge_ort_internal(opt, NULL, prev, next, result);
-		if (result->clean < 0)
+		if (result->clean < 0) {
+			free_commit_list(merge_bases);
 			return;
+		}

Before this function, there is a comment that this came from another function and it seems to still have a very similar code structure. Does the other function need the same fix, or if not why not?

Thanks.

#4 lore raw

PATCH v2 1/2 Regression test for https://github.com/gitgitgadget/git/pull/1577

In reply to Kevin Backhouse via GitGitGadget

From: Kevin Backhouse <[email protected]>

---
 t/t9904-merge-leak.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100755 t/t9904-merge-leak.sh
t/t9904-merge-leak.sh new file +40 -0
new file mode 100755
index 00000000000..09a4474fd73
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+
+test_description='regression test for memory leak in git merge'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-bash.sh
+
+# test-lib.sh disables LeakSanitizer by default, but we want it enabled
+# for this test
+ASAN_OPTIONS=
+export ASAN_OPTIONS
+
+. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
+
+test_expect_success 'Merge fails due to local changes' '
+	git init &&
+	echo x > x.txt &&
+	git add . &&
+	git commit -m "WIP" &&
+	git checkout -b dev &&
+	echo y > x.txt &&
+	git add . &&
+	git commit -m "WIP" &&
+	git checkout main &&
+	echo z > x.txt &&
+	git add . &&
+	git commit -m "WIP" &&
+	echo a > x.txt &&
+	git add . &&
+	echo "error: ''Your local changes to the following files would be overwritten by merge:''" >expected &&
+	echo "  x.txt" >>expected &&
+	echo "Merge with strategy ort failed." >>expected &&
+	test_must_fail git merge -s ort dev 2>actual &&
+	test_cmp expected actual
+'
+
+test_done
Signature · 1 line
gitgitgadget
#3 lore raw

v2 cover This fixes a minor memory leak (detected by LeakSanitizer) in git merge

In reply to Kevin Backhouse via GitGitGadget

Hi Junio,

Thank you for your comments. As you suggested, I have added similar fixes in merge-recursive.c and updated the commit message. I have also added a test.

Thanks,

Kev

Kevin Backhouse (2):
Regression test for https://github.com/gitgitgadget/git/pull/1577
Fix minor memory leak found by LeakSanitizer.

 merge-ort-wrappers.c  |  4 +++-
 merge-ort.c           |  4 +++-
 merge-recursive.c     | 32 ++++++++++++++++++++++----------
 t/t9904-merge-leak.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 12 deletions(-)
 create mode 100755 t/t9904-merge-leak.sh

base-commit: f9972720e9a405e4f6924a7cde0ed5880687f4d0
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1577%2Fkevinbackhouse%2Ffree-merge-bases-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1577/kevinbackhouse/free-merge-bases-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1577

Range-diff vs v1:

 -:  ----------- > 1:  f940104a781 Regression test for https://github.com/gitgitgadget/git/pull/1577
 1:  64b00e4448d ! 2:  353e1960b44 This fixes a minor memory leak (detected by LeakSanitizer) in git merge.

@@ Metadata
Author: Kevin Backhouse <[email protected]>

## Commit message ##
- This fixes a minor memory leak (detected by LeakSanitizer) in git merge.
+ Fix minor memory leak found by LeakSanitizer.

- To reproduce (with an ASAN build):
+ The callers of merge_recursive() and merge_ort_recursive() expects the
+ commit list passed in as the merge_bases parameter to be fully
+ consumed by the function and does not free it when the function
+ returns. In normal cases, the commit list does get consumed, but when
+ the function returns early upon encountering an error, it forgets to
+ clean it up.

- ```
- mkdir test
- cd test
- git init
- echo x > x.txt
- git add .
- git commit -m "WIP"
- git checkout -b dev
- echo y > x.txt
- git add .
- git commit -m "WIP"
- git checkout main
- echo z > x.txt
- git add .
- git commit -m "WIP"
- echo a > x.txt
- git add .
- git merge dev
- ```
-
- The fix is to call free_commit_list(merge_bases) when an error occurs.
+ Fix this by freeing the list in the code paths for error returns.

@@ merge-ort.c: static void merge_ort_internal(struct merge_options *opt,
opt->branch1 = saved_b1;
opt->branch2 = saved_b2;
opt->priv->call_depth--;
+
+ ## merge-recursive.c ##
+@@ merge-recursive.c: static int merge_recursive_internal(struct merge_options *opt,
+ opt->branch1 = "Temporary merge branch 1";
+ opt->branch2 = "Temporary merge branch 2";
+ if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
+- NULL, &merged_merge_bases) < 0)
+- return -1;
++ NULL, &merged_merge_bases) < 0) {
++ clean = -1;
++ goto out;
++ }
+ opt->branch1 = saved_b1;
+ opt->branch2 = saved_b2;
+ opt->priv->call_depth--;
+
+- if (!merged_merge_bases)
+- return err(opt, _("merge returned no commit"));
++ if (!merged_merge_bases) {
++ clean = err(opt, _("merge returned no commit"));
++ goto out;
++ }
+ }
+
+ /*
+@@ merge-recursive.c: static int merge_recursive_internal(struct merge_options *opt,
+ repo_get_commit_tree(opt->repo,
+ merged_merge_bases),
+ &result_tree);
++
++out:
+ strbuf_release(&merge_base_abbrev);
+ opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
++ free_commit_list(merge_bases);
+ if (clean < 0) {
+ flush_output(opt);
+ return clean;
+@@ merge-recursive.c: static int merge_start(struct merge_options *opt, struct tree *head)
+ assert(!opt->record_conflict_msgs_as_headers);
+ assert(!opt->msg_header_prefix);
+
++ CALLOC_ARRAY(opt->priv, 1);
++ string_list_init_dup(&opt->priv->df_conflict_file_set);
++
+ /* Sanity check on repo state; index must match head */
+ if (repo_index_has_changes(opt->repo, head, &sb)) {
+ err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
+@@ merge-recursive.c: static int merge_start(struct merge_options *opt, struct tree *head)
+ return -1;
+ }
+
+- CALLOC_ARRAY(opt->priv, 1);
+- string_list_init_dup(&opt->priv->df_conflict_file_set);
+ return 0;
+ }
+
+ static void merge_finalize(struct merge_options *opt)
+ {
+ flush_output(opt);
+- if (!opt->priv->call_depth && opt->buffer_output < 2)
+- strbuf_release(&opt->obuf);
++ strbuf_release(&opt->obuf);
+ if (show(opt, 2))
+ diff_warn_rename_limit("merge.renamelimit",
+ opt->priv->needed_rename_limit, 0);
+@@ merge-recursive.c: int merge_trees(struct merge_options *opt,
+
+ assert(opt->ancestor != NULL);
+
+- if (merge_start(opt, head))
++ if (merge_start(opt, head)) {
++ merge_finalize(opt);
+ return -1;
++ }
+ clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
+ merge_finalize(opt);
+
+@@ merge-recursive.c: int merge_recursive(struct merge_options *opt,
+ prepare_repo_settings(opt->repo);
+ opt->repo->settings.command_requires_full_index = 1;
+
+- if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
++ if (merge_start(opt, repo_get_commit_tree(opt->repo, h1))) {
++ free_commit_list(merge_bases);
++ merge_finalize(opt);
+ return -1;
++ }
+ clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
+ merge_finalize(opt);
+

Signature · 1 line
gitgitgadget
#6 lore raw

PATCH v2 2/2 Fix minor memory leak found by LeakSanitizer.

From: Kevin Backhouse <[email protected]>

The callers of merge_recursive() and merge_ort_recursive() expects the commit list passed in as the merge_bases parameter to be fully consumed by the function and does not free it when the function returns. In normal cases, the commit list does get consumed, but when the function returns early upon encountering an error, it forgets to clean it up.

Fix this by freeing the list in the code paths for error returns.

---
 merge-ort-wrappers.c |  4 +++-
 merge-ort.c          |  4 +++-
 merge-recursive.c    | 32 ++++++++++++++++++++++----------
 3 files changed, 28 insertions(+), 12 deletions(-)
merge-ort-wrappers.c +3 -1
index 4acedf3c338..aeb56c9970c 100644
@@ -54,8 +54,10 @@ int merge_ort_recursive(struct merge_options *opt,
 	struct tree *head = repo_get_commit_tree(opt->repo, side1);
 	struct merge_result tmp;
 
-	if (unclean(opt, head))
+	if (unclean(opt, head)) {
+		free_commit_list(merge_bases);
 		return -1;
+	}
 
 	memset(&tmp, 0, sizeof(tmp));
 	merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);
merge-ort.c +3 -1
index 8631c997002..a0eb91fb011 100644
@@ -5070,8 +5070,10 @@ static void merge_ort_internal(struct merge_options *opt,
 		opt->branch1 = "Temporary merge branch 1";
 		opt->branch2 = "Temporary merge branch 2";
 		merge_ort_internal(opt, NULL, prev, next, result);
-		if (result->clean < 0)
+		if (result->clean < 0) {
+			free_commit_list(merge_bases);
 			return;
+		}
 		opt->branch1 = saved_b1;
 		opt->branch2 = saved_b2;
 		opt->priv->call_depth--;
merge-recursive.c +22 -10
index 6a4081bb0f5..49e54d3722f 100644
@@ -3652,14 +3652,18 @@ static int merge_recursive_internal(struct merge_options *opt,
 		opt->branch1 = "Temporary merge branch 1";
 		opt->branch2 = "Temporary merge branch 2";
 		if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
-					     NULL, &merged_merge_bases) < 0)
-			return -1;
+					     NULL, &merged_merge_bases) < 0) {
+			clean = -1;
+			goto out;
+		}
 		opt->branch1 = saved_b1;
 		opt->branch2 = saved_b2;
 		opt->priv->call_depth--;
 
-		if (!merged_merge_bases)
-			return err(opt, _("merge returned no commit"));
+		if (!merged_merge_bases) {
+			clean = err(opt, _("merge returned no commit"));
+			goto out;
+		}
 	}
 
 	/*
@@ -3682,8 +3686,11 @@ static int merge_recursive_internal(struct merge_options *opt,
 				     repo_get_commit_tree(opt->repo,
 							  merged_merge_bases),
 				     &result_tree);
+
+out:
 	strbuf_release(&merge_base_abbrev);
 	opt->ancestor = NULL;  /* avoid accidental re-use of opt->ancestor */
+	free_commit_list(merge_bases);
 	if (clean < 0) {
 		flush_output(opt);
 		return clean;
@@ -3729,6 +3736,9 @@ static int merge_start(struct merge_options *opt, struct tree *head)
 	assert(!opt->record_conflict_msgs_as_headers);
 	assert(!opt->msg_header_prefix);
 
+	CALLOC_ARRAY(opt->priv, 1);
+	string_list_init_dup(&opt->priv->df_conflict_file_set);
+
 	/* Sanity check on repo state; index must match head */
 	if (repo_index_has_changes(opt->repo, head, &sb)) {
 		err(opt, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
@@ -3737,16 +3747,13 @@ static int merge_start(struct merge_options *opt, struct tree *head)
 		return -1;
 	}
 
-	CALLOC_ARRAY(opt->priv, 1);
-	string_list_init_dup(&opt->priv->df_conflict_file_set);
 	return 0;
 }
 
 static void merge_finalize(struct merge_options *opt)
 {
 	flush_output(opt);
-	if (!opt->priv->call_depth && opt->buffer_output < 2)
-		strbuf_release(&opt->obuf);
+	strbuf_release(&opt->obuf);
 	if (show(opt, 2))
 		diff_warn_rename_limit("merge.renamelimit",
 				       opt->priv->needed_rename_limit, 0);
@@ -3763,8 +3770,10 @@ int merge_trees(struct merge_options *opt,
 
 	assert(opt->ancestor != NULL);
 
-	if (merge_start(opt, head))
+	if (merge_start(opt, head)) {
+		merge_finalize(opt);
 		return -1;
+	}
 	clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
 	merge_finalize(opt);
 
@@ -3785,8 +3794,11 @@ int merge_recursive(struct merge_options *opt,
 	prepare_repo_settings(opt->repo);
 	opt->repo->settings.command_requires_full_index = 1;
 
-	if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
+	if (merge_start(opt, repo_get_commit_tree(opt->repo, h1))) {
+		free_commit_list(merge_bases);
+		merge_finalize(opt);
 		return -1;
+	}
 	clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
 	merge_finalize(opt);
 
Signature · 1 line
gitgitgadget
#5 lore raw

Re: [PATCH v2 1/2] Regression test for https://github.com/gitgitgadget/git/pull/1577

In reply to Kevin Backhouse via GitGitGadget
Quoted 1 line · Kevin Backhouse via GitGitGadget

Subject: Re: [PATCH v2 1/2] Regression test for https://github.com/gitgitgadget/git/pull/1577

We try to come up with titles that are helpful to readers when seen in "git shortlog --since=6.months --no-merges", and the above does not exactly it.

Quoted 18 lines

From: Kevin Backhouse <[email protected]>

---
 t/t9904-merge-leak.sh | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100755 t/t9904-merge-leak.sh
t/t9904-merge-leak.sh new file +4 -0
new file mode 100755
index 00000000000..09a4474fd73
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+
+test_description='regression test for memory leak in git merge'

t/t9904-merge-leak.sh line 14

Quoted 10 lines
t/t9904-merge-leak.sh new file +10 -0
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-bash.sh
+
+# test-lib.sh disables LeakSanitizer by default, but we want it enabled
+# for this test
+ASAN_OPTIONS=
+export ASAN_OPTIONS

You do not want to do this.

We have CI jobs that run everybody under asan, ubsan etc., so it is sufficient and much more preferrable to just add a reproduction recipe to an _existing_ test that is about "git merge" (or if we have "ort" specific one, "git merge -s ort"). Of course they would not fail in jobs that do not enable asan, and that is expected and perfectly OK.

Also, please check Documentation/CodingGuidelines for shell style issues.

t/t9904-merge-leak.sh line 16

Quoted 1 line
t/t9904-merge-leak.sh new file +1 -0
+. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"

Is this about testing prompts, or does the bug/leak appear only when the prompt support is in use? Could you explain why this is needed?

Quoted 11 lines
t/t9904-merge-leak.sh new file +11 -0
+test_expect_success 'Merge fails due to local changes' '
+	git init &&
+	echo x > x.txt &&
+	git add . &&
+	git commit -m "WIP" &&
+	git checkout -b dev &&
+	echo y > x.txt &&
+	git add . &&
+	git commit -m "WIP" &&
+	git checkout main &&
+	echo z > x.txt &&

t/t9904-merge-leak.sh line 38

Quoted 10 lines
t/t9904-merge-leak.sh new file +10 -0
+	git add . &&
+	git commit -m "WIP" &&
+	echo a > x.txt &&
+	git add . &&
+	echo "error: ''Your local changes to the following files would be overwritten by merge:''" >expected &&
+	echo "  x.txt" >>expected &&
+	echo "Merge with strategy ort failed." >>expected &&
+	test_must_fail git merge -s ort dev 2>actual &&
+	test_cmp expected actual
+'

If this 1/2 adds a new test that is expected to fail without leak fix, which has to wait until 2/2, it breaks the bisection. In this case, since it will be a simple addition to an existing test script, having both tests and code changes in a single patch is the most appropriate.

Thank you for working on this.

Quoted 2 lines
t/t9904-merge-leak.sh new file +2 -0
+
+test_done
#7 lore raw

Re: [PATCH v2 2/2] Fix minor memory leak found by LeakSanitizer.

In reply to Kevin Backhouse via GitGitGadget
Quoted 1 line · Kevin Backhouse via GitGitGadget

Subject: Re: [PATCH v2 2/2] Fix minor memory leak found by LeakSanitizer.

Continuing the review for the previous step, perhaps

Subject: [PATCH] merge: free list of merge bases upon failure

or something?

Quoted 3 lines

From: Kevin Backhouse <[email protected]>

The callers of merge_recursive() and merge_ort_recursive() expects the

"expects" -> "expect"

Quoted 2 lines

commit list passed in as the merge_bases parameter to be fully consumed by the function and does not free it when the function

"does not" -> "do not".

Quoted 8 lines

returns. In normal cases, the commit list does get consumed, but when the function returns early upon encountering an error, it forgets to clean it up.

Fix this by freeing the list in the code paths for error returns.

---

Well written to be understandable. Nicely done.

Quoted 10 lines
 merge-ort-wrappers.c |  4 +++-
 merge-ort.c          |  4 +++-
 merge-recursive.c    | 32 ++++++++++++++++++++++----------
 3 files changed, 28 insertions(+), 12 deletions(-)
merge-ort-wrappers.c +0 -0
index 4acedf3c338..aeb56c9970c 100644

merge-ort-wrappers.c int merge_ort_recursive(struct merge_options *opt, · line 60

Quoted 8 lines
merge-ort-wrappers.c +3 -1
@@ -54,8 +54,10 @@ int merge_ort_recursive(struct merge_options *opt,
 	struct tree *head = repo_get_commit_tree(opt->repo, side1);
 	struct merge_result tmp;
 
-	if (unclean(opt, head))
+	if (unclean(opt, head)) {
+		free_commit_list(merge_bases);
 		return -1;
+	}

OK.

merge-ort.c static void merge_ort_internal(struct merge_options *opt, · line 5076

Quoted 13 lines
merge-ort.c +3 -1
index 8631c997002..a0eb91fb011 100644
@@ -5070,8 +5070,10 @@ static void merge_ort_internal(struct merge_options *opt,
 		opt->branch1 = "Temporary merge branch 1";
 		opt->branch2 = "Temporary merge branch 2";
 		merge_ort_internal(opt, NULL, prev, next, result);
-		if (result->clean < 0)
+		if (result->clean < 0) {
+			free_commit_list(merge_bases);
 			return;
+		}

OK.

Quoted 29 lines
merge-recursive.c +8 -4
index 6a4081bb0f5..49e54d3722f 100644
@@ -3652,14 +3652,18 @@ static int merge_recursive_internal(struct merge_options *opt,
 		opt->branch1 = "Temporary merge branch 1";
 		opt->branch2 = "Temporary merge branch 2";
 		if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
-					     NULL, &merged_merge_bases) < 0)
-			return -1;
+					     NULL, &merged_merge_bases) < 0) {
+			clean = -1;
+			goto out;
+		}
 		opt->branch1 = saved_b1;
 		opt->branch2 = saved_b2;
 		opt->priv->call_depth--;
 
-		if (!merged_merge_bases)
-			return err(opt, _("merge returned no commit"));
+		if (!merged_merge_bases) {
+			clean = err(opt, _("merge returned no commit"));
+			goto out;
+		}
 	}
 
 	/*
@@ -3682,8 +3686,11 @@ static int merge_recursive_internal(struct merge_options *opt,
 				     repo_get_commit_tree(opt->repo,

merge-recursive.c static int merge_recursive_internal(struct merge_options *opt, · line 3696

Quoted 10 lines
merge-recursive.c +3 -0
 							  merged_merge_bases),
 				     &result_tree);
+
+out:
 	strbuf_release(&merge_base_abbrev);
 	opt->ancestor = NULL;  /* avoid accidental re-use of opt->ancestor */
+	free_commit_list(merge_bases);
 	if (clean < 0) {
 		flush_output(opt);
 		return clean;

Hmph, so the proposed log message made it sound like the merge_bases list is consumed fully in the normal non-error case, but even the normal case was leaky on the "-s recursive" side? Or was the recursive side was OK and the caller had different expectations, in which case we may be breaking them, but you poked at these codepaths long enough to produce this patch, so I doubt it. The proposed log message needs to be updated to explain the findings on this side, too, if the situation is different from the "ort" side.

merge-recursive.c static int merge_start(struct merge_options *opt, struct tree *head) · line 3740

Quoted 6 lines
merge-recursive.c +2 -0
@@ -3729,6 +3736,9 @@ static int merge_start(struct merge_options *opt, struct tree *head)
 	assert(!opt->record_conflict_msgs_as_headers);
 	assert(!opt->msg_header_prefix);
 
+	CALLOC_ARRAY(opt->priv, 1);
+	string_list_init_dup(&opt->priv->df_conflict_file_set);

This move, what it does, why it is needed, and what breaks without it, is not explained in the proposed log message.

Quoted 8 lines
merge-recursive.c +0 -1
 	/* Sanity check on repo state; index must match head */
 	if (repo_index_has_changes(opt->repo, head, &sb)) {
 		err(opt, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
@@ -3737,16 +3747,13 @@ static int merge_start(struct merge_options *opt, struct tree *head)
 		return -1;
 	}
 
-	CALLOC_ARRAY(opt->priv, 1);

merge-recursive.c static int merge_start(struct merge_options *opt, struct tree *head) · line 3756

Quoted 10 lines
merge-recursive.c +1 -3
-	string_list_init_dup(&opt->priv->df_conflict_file_set);
 	return 0;
 }
 
 static void merge_finalize(struct merge_options *opt)
 {
 	flush_output(opt);
-	if (!opt->priv->call_depth && opt->buffer_output < 2)
-		strbuf_release(&opt->obuf);
+	strbuf_release(&opt->obuf);

Ditto. Unconditional release here may help the new caller in merge_trees() that failed merge_start(), but is the change safe for other existing callers and if so why/how?

In any case, this needs a review by somebody more familiar with the recursive backend machinery than myself. Any takers?

Quoted 16 lines
merge-recursive.c +3 -1
 	if (show(opt, 2))
 		diff_warn_rename_limit("merge.renamelimit",
 				       opt->priv->needed_rename_limit, 0);
@@ -3763,8 +3770,10 @@ int merge_trees(struct merge_options *opt,
 
 	assert(opt->ancestor != NULL);
 
-	if (merge_start(opt, head))
+	if (merge_start(opt, head)) {
+		merge_finalize(opt);
 		return -1;
+	}
 	clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
 	merge_finalize(opt);
 

merge-recursive.c int merge_recursive(struct merge_options *opt, · line 3801

Quoted 9 lines
merge-recursive.c +4 -1
@@ -3785,8 +3794,11 @@ int merge_recursive(struct merge_options *opt,
 	prepare_repo_settings(opt->repo);
 	opt->repo->settings.command_requires_full_index = 1;
 
-	if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
+	if (merge_start(opt, repo_get_commit_tree(opt->repo, h1))) {
+		free_commit_list(merge_bases);
+		merge_finalize(opt);
 		return -1;
+	}

I suspect that the way leaks happen is different between "ort" and "recursive", and what is in the proposed log message may have been the right description of the problem back when the patch was only about fixing "ort" but no longer is sufficient now that we also fix the "recursive" side.

merge-recursive.c int merge_recursive(struct merge_options *opt, · line 3803

Quoted 2 lines
merge-recursive.c +0 -0
 	clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
 	merge_finalize(opt);

Hmph, but this does expect merge_bases is consumed in normal codepath. Now I am confused, sorry.

Thanks for working on this.

#2 lore raw
In reply to Junio C Hamano
Quoted 80 lines · Junio C Hamano
Quoted 15 lines · Kevin Backhouse via GitGitGadget

From: Kevin Backhouse <[email protected]>

To reproduce (with an ASAN build):

```
mkdir test
cd test
git init
echo x > x.txt
git add .
git commit -m "WIP"
git checkout -b dev
echo y > x.txt
git add .
git commit -m "WIP"
Quoted 8 lines
git checkout main
echo z > x.txt
git add .
git commit -m "WIP"
echo a > x.txt
git add .
git merge dev
```

We'd rather not to see the above in the proposed log message; can't we add (a variation of) it to our test suite?

Quoted 1 line

The fix is to call free_commit_list(merge_bases) when an error occurs.

We usually have the description of what the problem is and give an analysis on why/how it happens, before presenting a solution. Write it more like:

The caller of merge_ort_recursive() expects the commit list passed in as the merge_bases parameter to be fully consumed by the function and does not free it when the function returns. In normal cases, the commit list does get consumed, but when the function returns early upon encountering an error, it forgets to clean it up.

Fix this by freeing the list in the code paths for error returns.

Quoted 2 lines
 merge-ort-wrappers.c | 4 +++-
 merge-ort.c          | 4 +++-

These two places and their fixes seem OK, but I have to wonder if these are complete fixes.

Quoted 6 lines
merge-ort-wrappers.c +0 -0
index 4acedf3c338..aeb56c9970c 100644
@@ -54,8 +54,10 @@ int merge_ort_recursive(struct merge_options *opt,
      struct tree *head = repo_get_commit_tree(opt->repo, side1);

merge-ort-wrappers.c int merge_ort_recursive(struct merge_options *opt, · line 63

Quoted 10 lines
merge-ort-wrappers.c +3 -1
      struct merge_result tmp;
 
-     if (unclean(opt, head))
+     if (unclean(opt, head)) {
+             free_commit_list(merge_bases);
              return -1;
+     }
 
      memset(&tmp, 0, sizeof(tmp));
      merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);

The function before this hunk appears to have very similar code structure. Does it need the same fix, or if not why not?

Quoted 9 lines
merge-ort.c +0 -1
index 8631c997002..a0eb91fb011 100644
@@ -5070,8 +5070,10 @@ static void merge_ort_internal(struct merge_options *opt,
              opt->branch1 = "Temporary merge branch 1";
              opt->branch2 = "Temporary merge branch 2";
              merge_ort_internal(opt, NULL, prev, next, result);
-             if (result->clean < 0)
Quoted 8 lines

merge-ort.c static void merge_ort_internal(struct merge_options *opt, · line 5076

Quoted 4 lines
merge-ort.c +3 -0
+             if (result->clean < 0) {
+                     free_commit_list(merge_bases);
                      return;
+             }

Before this function, there is a comment that this came from another function and it seems to still have a very similar code structure. Does the other function need the same fix, or if not why not?

The other function would need a more involved fix, which would basically involve porting a59b8dd94f (merge-ort: fix memory leak in merge_ort_internal(), 2022-01-20) to merge-recursive as a preparatory step. This particular cleanup cannot be ported in its current form to merge-recursive.c until then.