Skip to content

Commit 4da7264

Browse files
Martin Ågrengitster
authored andcommitted
reduce_heads: fix memory leaks
We currently have seven callers of `reduce_heads(foo)`. Six of them do not use the original list `foo` again, and actually, all six of those end up leaking it. Introduce and use `reduce_heads_replace(&foo)` as a leak-free version of `foo = reduce_heads(foo)` to fix several of these. Fix the remaining leaks using `free_commit_list()`. While we're here, document `reduce_heads()` and mark it as `extern`. Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a452d0f commit 4da7264

File tree

7 files changed

+35
-6
lines changed

7 files changed

+35
-6
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17281728
allow_fast_forward = 0;
17291729
}
17301730
if (allow_fast_forward)
1731-
parents = reduce_heads(parents);
1731+
reduce_heads_replace(&parents);
17321732
} else {
17331733
if (!reflog_msg)
17341734
reflog_msg = (whence == FROM_CHERRY_PICK)

builtin/fmt-merge-msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ static void find_merge_parents(struct merge_parents *result,
571571
head_commit = lookup_commit(head);
572572
if (head_commit)
573573
commit_list_insert(head_commit, &parents);
574-
parents = reduce_heads(parents);
574+
reduce_heads_replace(&parents);
575575

576576
while (parents) {
577577
struct commit *cmit = pop_commit(&parents);

builtin/merge-base.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static int handle_independent(int count, const char **args)
5757
for (i = count - 1; i >= 0; i--)
5858
commit_list_insert(get_commit_reference(args[i]), &revs);
5959

60-
revs = reduce_heads(revs);
60+
reduce_heads_replace(&revs);
6161

6262
if (!revs)
6363
return 1;
@@ -78,7 +78,9 @@ static int handle_octopus(int count, const char **args, int show_all)
7878
for (i = count - 1; i >= 0; i--)
7979
commit_list_insert(get_commit_reference(args[i]), &revs);
8080

81-
result = reduce_heads(get_octopus_merge_bases(revs));
81+
result = get_octopus_merge_bases(revs);
82+
free_commit_list(revs);
83+
reduce_heads_replace(&result);
8284

8385
if (!result)
8486
return 1;

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
999999

10001000
/* Find what parents to record by checking independent ones. */
10011001
parents = reduce_heads(remoteheads);
1002+
free_commit_list(remoteheads);
10021003

10031004
remoteheads = NULL;
10041005
remotes = &remoteheads;

builtin/pull.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,15 @@ static int get_octopus_merge_base(struct object_id *merge_base,
745745
if (!is_null_oid(fork_point))
746746
commit_list_insert(lookup_commit_reference(fork_point), &revs);
747747

748-
result = reduce_heads(get_octopus_merge_bases(revs));
748+
result = get_octopus_merge_bases(revs);
749749
free_commit_list(revs);
750+
reduce_heads_replace(&result);
751+
750752
if (!result)
751753
return 1;
752754

753755
oidcpy(merge_base, &result->item->object.oid);
756+
free_commit_list(result);
754757
return 0;
755758
}
756759

commit.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,13 @@ struct commit_list *reduce_heads(struct commit_list *heads)
10901090
return result;
10911091
}
10921092

1093+
void reduce_heads_replace(struct commit_list **heads)
1094+
{
1095+
struct commit_list *result = reduce_heads(*heads);
1096+
free_commit_list(*heads);
1097+
*heads = result;
1098+
}
1099+
10931100
static const char gpg_sig_header[] = "gpgsig";
10941101
static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
10951102

commit.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,23 @@ extern int interactive_add(int argc, const char **argv, const char *prefix, int
313313
extern int run_add_interactive(const char *revision, const char *patch_mode,
314314
const struct pathspec *pathspec);
315315

316-
struct commit_list *reduce_heads(struct commit_list *heads);
316+
/*
317+
* Takes a list of commits and returns a new list where those
318+
* have been removed that can be reached from other commits in
319+
* the list. It is useful for, e.g., reducing the commits
320+
* randomly thrown at the git-merge command and removing
321+
* redundant commits that the user shouldn't have given to it.
322+
*
323+
* This function destroys the STALE bit of the commit objects'
324+
* flags.
325+
*/
326+
extern struct commit_list *reduce_heads(struct commit_list *heads);
327+
328+
/*
329+
* Like `reduce_heads()`, except it replaces the list. Use this
330+
* instead of `foo = reduce_heads(foo);` to avoid memory leaks.
331+
*/
332+
extern void reduce_heads_replace(struct commit_list **heads);
317333

318334
struct commit_extra_header {
319335
struct commit_extra_header *next;

0 commit comments

Comments
 (0)