Skip to content

Commit 4867f11

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 833e482 commit 4867f11

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

xdiff/xmerge.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,11 @@ int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2,
579579
result->ptr = NULL;
580580
result->size = 0;
581581

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);
584587
return -1;
585588
}
586589
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,
592595
if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 ||
593596
xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 ||
594597
xdl_build_script(&xe2, &xscr2) < 0) {
598+
xdl_free_script(xscr1);
599+
xdl_free_env(&xe1);
595600
xdl_free_env(&xe2);
596601
return -1;
597602
}

0 commit comments

Comments
 (0)