# [PATCH] commit: fix memory leak in `reduce_heads()`

3 messages from 2017-09-20 to 2017-09-20. Participants: Martin Ågren, Jeff King, Jonathan Nieder.
Thread: https://gitlist.dev/t/46792

## Martin Ågren, 2017-09-20 19:47

Subject: [PATCH] commit: fix memory leak in `reduce_heads()`
Message-ID: <1505936846-2195-3-git-send-email-martin.agren@gmail.com>
URL: https://gitlist.dev/e/1505936846-2195-3-git-send-email-martin.agren%40gmail.com

```
We don't free the temporary scratch space we use with
`remove_redundant()`. Free it similar to how we do it in
`get_merge_bases_many_0()`.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 commit.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/commit.c b/commit.c
index 9062980..1e0e633 100644
--- a/commit.c
+++ b/commit.c
@@ -1086,6 +1086,7 @@ struct commit_list *reduce_heads(struct commit_list *heads)
 	num_head = remove_redundant(array, num_head);
 	for (i = 0; i < num_head; i++)
 		tail = &commit_list_insert(array[i], tail)->next;
+	free(array);
 	return result;
 }
 
-- 
2.14.1


```

## Jeff King, 2017-09-20 20:15

Subject: Re: [PATCH] commit: fix memory leak in `reduce_heads()`
Message-ID: <20170920201532.paynyzq7ln7cadqc@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170920201532.paynyzq7ln7cadqc%40sigill.intra.peff.net
In-Reply-To: <1505936846-2195-3-git-send-email-martin.agren@gmail.com>

```
On Wed, Sep 20, 2017 at 09:47:25PM +0200, Martin Ågren wrote:

> We don't free the temporary scratch space we use with
> `remove_redundant()`. Free it similar to how we do it in
> `get_merge_bases_many_0()`.

Yep, seems very straightforward, and there are no code paths where we
wouldn't want this.

-Peff

```

## Jonathan Nieder, 2017-09-20 21:34

Subject: Re: [PATCH] commit: fix memory leak in `reduce_heads()`
Message-ID: <20170920213417.GG27425@aiede.mtv.corp.google.com>
URL: https://gitlist.dev/e/20170920213417.GG27425%40aiede.mtv.corp.google.com
In-Reply-To: <1505936846-2195-3-git-send-email-martin.agren@gmail.com>

```
Martin Ågren wrote:

> We don't free the temporary scratch space we use with
> `remove_redundant()`. Free it similar to how we do it in
> `get_merge_bases_many_0()`.
>
> Signed-off-by: Martin Ågren <martin.agren@gmail.com>
> ---
>  commit.c | 1 +
>  1 file changed, 1 insertion(+)

Good catch.  Thanks.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

```
