Skip to content

Commit 8d60e9d

Browse files
newrengitster
authored andcommitted
merge-ort: fix small memory leak in detect_and_process_renames()
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 <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e6ebfd0 commit 8d60e9d

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

merge-ort.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,12 +3086,11 @@ static int detect_and_process_renames(struct merge_options *opt,
30863086
struct tree *side1,
30873087
struct tree *side2)
30883088
{
3089-
struct diff_queue_struct combined;
3089+
struct diff_queue_struct combined = { 0 };
30903090
struct rename_info *renames = &opt->priv->renames;
3091-
int need_dir_renames, s, clean = 1;
3091+
int need_dir_renames, s, i, clean = 1;
30923092
unsigned detection_run = 0;
30933093

3094-
memset(&combined, 0, sizeof(combined));
30953094
if (!possible_renames(renames))
30963095
goto cleanup;
30973096

@@ -3175,13 +3174,9 @@ static int detect_and_process_renames(struct merge_options *opt,
31753174
free(renames->pairs[s].queue);
31763175
DIFF_QUEUE_CLEAR(&renames->pairs[s]);
31773176
}
3178-
if (combined.nr) {
3179-
int i;
3180-
for (i = 0; i < combined.nr; i++)
3181-
pool_diff_free_filepair(&opt->priv->pool,
3182-
combined.queue[i]);
3183-
free(combined.queue);
3184-
}
3177+
for (i = 0; i < combined.nr; i++)
3178+
pool_diff_free_filepair(&opt->priv->pool, combined.queue[i]);
3179+
free(combined.queue);
31853180

31863181
return clean;
31873182
}

0 commit comments

Comments
 (0)