xdiff/xmerge: fix memory leak in xdl_merge

When building the script for the second file that is to be merged we have already allocated memory for data structures related to the first file. When we encounter an error in building the second script we only free allocated memory related to the second file before erroring out. Fix this memory leak by also releasing allocated memory related to the first file. Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 23, 2016 at 12:59 UTC 4867f1184c943f7738bb7b42cab2e62cf16cdb18
1 file changed +7 -2
xdiff/xmerge.c
+7 -2
@@ -579,8 +579,11 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
579 result->ptr = NULL;
580 result->size = 0;
581
582 - if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 ||
583 - xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
582 + if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) {
583 + return -1;
584 + }
585 + if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
586 + xdl_free_env(&xe1);
587 return -1;
588 }
589 if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 ||
@@ -592,6 +595,8 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
595 if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
596 xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
597 xdl_build_script(&xe2, &xscr2) < 0) {
598 + xdl_free_script(xscr1);
599 + xdl_free_env(&xe1);
600 xdl_free_env(&xe2);
601 return -1;
602 }