xdiff/xmerge: fix memory leak in xdl_merge
authorPatrick Steinhardt <ps@pks.im>
Tue, 23 Feb 2016 11:59:17 +0000 (12:59 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 23 Feb 2016 20:58:26 +0000 (12:58 -0800)
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>
xdiff/xmerge.c
index 625198e0585c52c20c778790c82126efd1b57009..e272424cfba7b33b821a930a2064d4a3d2e9a6f8 100644 (file)
@@ -579,8 +579,11 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
        result->ptr = NULL;
        result->size = 0;
 
-       if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0 ||
-                       xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
+       if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) {
+               return -1;
+       }
+       if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) {
+               xdl_free_env(&xe1);
                return -1;
        }
        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,
        if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
            xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
            xdl_build_script(&xe2, &xscr2) < 0) {
+               xdl_free_script(xscr1);
+               xdl_free_env(&xe1);
                xdl_free_env(&xe2);
                return -1;
        }