merge_trees(): ensure that the callers release output buffer

The recursive merge machinery accumulates its output in an output buffer, to be flushed at the end of merge_recursive(). At this point, we forgot to release the output buffer. When calling merge_trees() (i.e. the non-recursive part of the recursive merge) directly, the output buffer is never flushed because the caller may be merge_recursive() which wants to flush the output itself. For the same reason, merge_trees() cannot release the output buffer: it may still be needed. Forgetting to release the output buffer did not matter much when running git-checkout, or git-merge-recursive, because we exited after the operation anyway. Ever since cherry-pick learned to pick a commit range, however, this memory leak had the potential of becoming a problem. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 1, 2016 at 13:44 UTC 548009c0d58c74dde2c51f675b369b4c50878f1b
3 files changed +4
builtin/checkout.c
+1
@@ -573,6 +573,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
573 exit(128);
574 ret = reset_tree(new->commit->tree, opts, 0,
575 writeout_error);
576 + strbuf_release(&o.obuf);
577 if (ret)
578 return ret;
579 }
merge-recursive.c
+2
@@ -2078,6 +2078,8 @@ int merge_recursive(struct merge_options *o,
2078 commit_list_insert(h2, &(*result)->parents->next);
2079 }
2080 flush_output(o);
2081 + if (!o->call_depth && o->buffer_output < 2)
2082 + strbuf_release(&o->obuf);
2083 if (show(o, 2))
2084 diff_warn_rename_limit("merge.renamelimit",
2085 o->needed_rename_limit, 0);
sequencer.c
+1
@@ -293,6 +293,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
293 clean = merge_trees(&o,
294 head_tree,
295 next_tree, base_tree, &result);
296 + strbuf_release(&o.obuf);
297 if (clean < 0)
298 return clean;
299