Subject: Re: [PATCH 17/30] merge-recursive: Fix leaks of allocated renames and diff_filepairs

From: Junio C Hamano <[email-masked]>
Date: Tue, 14 Nov 2017 13:58:18 +0900

Elijah Newren <[email-masked]> writes:

> get_renames() has always zero'ed out diff_queued_diff.nr while only
> manually free'ing diff_filepairs that did not correspond to renames.
> Further, it allocated struct renames that were tucked away in the
> return string_list.  Make sure all of these are deallocated when we
> are done with them.
>
> Signed-off-by: Elijah Newren <[email-masked]>
> ---
>  merge-recursive.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/merge-recursive.c b/merge-recursive.c
> index 49710c0964..7a3402e50c 100644
> --- a/merge-recursive.c
> +++ b/merge-recursive.c
> @@ -1661,10 +1661,21 @@ static struct rename_info *handle_renames(struct merge_options *o,
>  
>  static void cleanup_renames(struct rename_info *re_info)
>  {
> -	string_list_clear(re_info->head_renames, 0);
> -	string_list_clear(re_info->merge_renames, 0);
> +	const struct rename *re;
> +	int i;
>  
> +	for (i = 0; i < re_info->head_renames->nr; i++) {
> +		re = re_info->head_renames->items[i].util;
> +		diff_free_filepair(re->pair);
> +	}
> +	string_list_clear(re_info->head_renames, 1);
>  	free(re_info->head_renames);
> +
> +	for (i = 0; i < re_info->merge_renames->nr; i++) {
> +		re = re_info->merge_renames->items[i].util;
> +		diff_free_filepair(re->pair);
> +	}
> +	string_list_clear(re_info->merge_renames, 1);

And this obviously will be helped by having another helper "cleanup_rename()"
that does one of them, and call it twice from here.