Skip to content

Commit 6046f7a

Browse files
newrengitster
authored andcommitted
merge: fix memory leaks in cmd_merge()
There were two commit_lists created in cmd_merge() that were only conditionally free()'d. Add a quick conditional call to free_commit_list() for each of them at the end of the function. Testing this commit against t6404 under valgrind shows that this patch fixes the following two leaks: 16 bytes in 1 blocks are definitely lost in loss record 16 of 126 at 0x484086F: malloc (vg_replace_malloc.c:380) by 0x69FFEB: do_xmalloc (wrapper.c:41) by 0x6A0073: xmalloc (wrapper.c:62) by 0x52A72D: commit_list_insert (commit.c:556) by 0x47FC93: reduce_parents (merge.c:1114) by 0x4801EE: collect_parents (merge.c:1214) by 0x480B56: cmd_merge (merge.c:1465) by 0x40686E: run_builtin (git.c:464) by 0x406C51: handle_builtin (git.c:716) by 0x406E96: run_argv (git.c:783) by 0x40730A: cmd_main (git.c:914) by 0x4E7DFA: main (common-main.c:56) 8 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in \ loss record 61 of 126 at 0x484086F: malloc (vg_replace_malloc.c:380) by 0x69FFEB: do_xmalloc (wrapper.c:41) by 0x6A0073: xmalloc (wrapper.c:62) by 0x52A72D: commit_list_insert (commit.c:556) by 0x52A8F2: commit_list_insert_by_date (commit.c:620) by 0x5270AC: get_merge_bases_many_0 (commit-reach.c:413) by 0x52716C: repo_get_merge_bases (commit-reach.c:438) by 0x480E5A: cmd_merge (merge.c:1520) by 0x40686E: run_builtin (git.c:464) by 0x406C51: handle_builtin (git.c:716) by 0x406E96: run_argv (git.c:783) by 0x40730A: cmd_main (git.c:914) There are still 3 leaks in chdir_notify_register() after this, but chdir_notify_register() has been brought up on the list before and folks were not a fan of fixing those, so I'm not touching them. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a59b8dd commit 6046f7a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

builtin/merge.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12731273
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
12741274
struct commit_list *common = NULL;
12751275
const char *best_strategy = NULL, *wt_strategy = NULL;
1276-
struct commit_list *remoteheads, *p;
1276+
struct commit_list *remoteheads = NULL, *p;
12771277
void *branch_to_free;
12781278
int orig_argc = argc;
12791279

@@ -1752,6 +1752,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
17521752
ret = suggest_conflicts();
17531753

17541754
done:
1755+
if (!automerge_was_ok) {
1756+
free_commit_list(common);
1757+
free_commit_list(remoteheads);
1758+
}
17551759
strbuf_release(&buf);
17561760
free(branch_to_free);
17571761
return ret;

0 commit comments

Comments
 (0)