merge-recursive: fix memory leak

In merge_trees if process_renames or process_entry returns less than zero, the method will just return and not free re_merge, re_head, or entries. This change cleans up the allocated variables before returning to the caller. Signed-off-by: Kevin Willford <kewillf@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kevin Willford committed Aug 28, 2017 at 14:28 UTC e336bdc5b9bcb62982da9708dfb6e68150de72a3
1 file changed +9 -3
merge-recursive.c
+9 -3
@@ -1956,7 +1956,7 @@ int merge_trees(struct merge_options *o,
1956 re_merge = get_renames(o, merge, common, head, merge, entries);
1957 clean = process_renames(o, re_head, re_merge);
1958 if (clean < 0)
1959 - return clean;
1959 + goto cleanup;
1960 for (i = entries->nr-1; 0 <= i; i--) {
1961 const char *path = entries->items[i].string;
1962 struct stage_data *e = entries->items[i].util;
@@ -1964,8 +1964,10 @@ int merge_trees(struct merge_options *o,
1964 int ret = process_entry(o, path, e);
1965 if (!ret)
1966 clean = 0;
1967 - else if (ret < 0)
1968 - return ret;
1967 + else if (ret < 0) {
1968 + clean = ret;
1969 + goto cleanup;
1970 + }
1971 }
1972 }
1973 for (i = 0; i < entries->nr; i++) {
@@ -1975,6 +1977,7 @@ int merge_trees(struct merge_options *o,
1977 entries->items[i].string);
1978 }
1979
1980 +cleanup:
1981 string_list_clear(re_merge, 0);
1982 string_list_clear(re_head, 0);
1983 string_list_clear(entries, 1);
@@ -1982,6 +1985,9 @@ int merge_trees(struct merge_options *o,
1985 free(re_merge);
1986 free(re_head);
1987 free(entries);
1988 +
1989 + if (clean < 0)
1990 + return clean;
1991 }
1992 else
1993 clean = 1;