git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/2] merge-ort: fix small memory leak in detect_and_process_renames()

From
Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 20, 2022, 01:29 UTC
Message-ID
<f1f7fc97fe2fe5079365bb91c71fb7033378995d.1645320592.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1152.v2.git.1645320591.gitgitgadget@gmail.com>
From: Elijah Newren <newren@gmail.com>

detect_and_process_renames() detects renames on both sides of history and then combines these into a single diff_queue_struct. The combined diff_queue_struct needs to be able to hold the renames found on either side, and since it knows the (maximum) size it needs, it pre-emptively grows the array to the appropriate size:

	ALLOC_GROW(combined.queue,
		   renames->pairs[1].nr + renames->pairs[2].nr,
		   combined.alloc);
It then collects the items from each side:
	collect_renames(opt, &combined, MERGE_SIDE1, ...)
	collect_renames(opt, &combined, MERGE_SIDE2, ...)

Note, though, that collect_renames() sometimes determines that some pairs are unnecessary and does not include them in the combined array. When it is done, detect_and_process_renames() frees this memory:

	if (combined.nr) {
                ...
		free(combined.queue);
        }

The problem is that sometimes even when there are pairs, none of them are necessary. Instead of checking combined.nr, just remove the if-check; free() knows to skip NULL pointers. This change fixes the following memory leak, as reported by valgrind:

==PID== 192 bytes in 1 blocks are definitely lost in loss record 107 of 134 ==PID== at 0xADDRESS: malloc ==PID== by 0xADDRESS: realloc ==PID== by 0xADDRESS: xrealloc (wrapper.c:126) ==PID== by 0xADDRESS: detect_and_process_renames (merge-ort.c:3134) ==PID== by 0xADDRESS: merge_ort_nonrecursive_internal (merge-ort.c:4610) ==PID== by 0xADDRESS: merge_ort_internal (merge-ort.c:4709) ==PID== by 0xADDRESS: merge_incore_recursive (merge-ort.c:4760) ==PID== by 0xADDRESS: merge_ort_recursive (merge-ort-wrappers.c:57) ==PID== by 0xADDRESS: try_merge_strategy (merge.c:753) ==PID== by 0xADDRESS: cmd_merge (merge.c:1676) ==PID== by 0xADDRESS: run_builtin (git.c:461) ==PID== by 0xADDRESS: handle_builtin (git.c:713) ==PID== by 0xADDRESS: run_argv (git.c:780) ==PID== by 0xADDRESS: cmd_main (git.c:911) ==PID== by 0xADDRESS: main (common-main.c:52)

Reported-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/merge-ort.c b/merge-ort.c
index d85b1cd99e9..3d7f9feb6f7 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -3086,12 +3086,11 @@ static int detect_and_process_renames(struct merge_options *opt,
 				      struct tree *side1,
 				      struct tree *side2)
 {
-	struct diff_queue_struct combined;
+	struct diff_queue_struct combined = { 0 };
 	struct rename_info *renames = &opt->priv->renames;
-	int need_dir_renames, s, clean = 1;
+	int need_dir_renames, s, i, clean = 1;
 	unsigned detection_run = 0;
 
-	memset(&combined, 0, sizeof(combined));
 	if (!possible_renames(renames))
 		goto cleanup;
 
@@ -3175,13 +3174,9 @@ simple_cleanup:
 		free(renames->pairs[s].queue);
 		DIFF_QUEUE_CLEAR(&renames->pairs[s]);
 	}
-	if (combined.nr) {
-		int i;
-		for (i = 0; i < combined.nr; i++)
-			pool_diff_free_filepair(&opt->priv->pool,
-						combined.queue[i]);
-		free(combined.queue);
-	}
+	for (i = 0; i < combined.nr; i++)
+		pool_diff_free_filepair(&opt->priv->pool, combined.queue[i]);
+	free(combined.queue);
 
 	return clean;
 }
-- 
gitgitgadget
Previous: Elijah Newren via GitGitGadgetNext: Taylor Blau
Message 9 of 15 in “Fix a couple small leaks in merge-ort”
  1. 0/2 Fix a couple small leaks in merge-ortElijah Newren via GitGitGadget, Feb 19, 2022
  2. 1/2 merge-ort: fix small memory leak in detect_and_process_renames()Elijah Newren via GitGitGadget, Feb 19, 2022
  3. Ævar Arnfjörð BjarmasonFeb 19, 2022
  4. Elijah NewrenFeb 20, 2022
  5. 2/2 merge-ort: fix small memory leak in unique_path()Elijah Newren via GitGitGadget, Feb 19, 2022
  6. Ævar Arnfjörð BjarmasonFeb 19, 2022
  7. Elijah NewrenFeb 20, 2022
  8. 0/2 Fix a couple small leaks in merge-ortElijah Newren via GitGitGadget, Feb 20, 2022
  9. 1/2 merge-ort: fix small memory leak in detect_and_process_renames()Elijah Newren via GitGitGadget, Feb 20, 2022
  10. Taylor BlauFeb 21, 2022
  11. Elijah NewrenFeb 23, 2022
  12. Ævar Arnfjörð BjarmasonJun 1, 2022
  13. Flaky SANITIZE=leak test "regression" in v2.36.0 (was: [PATCH v2 1/2] merge-ort: fix small memory leak in detect_and_process_renames())Ævar Arnfjörð Bjarmason, Jun 1, 2022
  14. 2/2 merge-ort: fix small memory leak in unique_path()Elijah Newren via GitGitGadget, Feb 20, 2022
  15. Taylor BlauFeb 21, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.