git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] xdiff/xmerge: fix memory leak in xdl_merge

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 23, 2016, 11:59 UTC
Message-ID
<1456228757-19333-1-git-send-email-ps@pks.im>
In-Reply-To
<1456217151-16473-1-git-send-email-ps@pks.im>

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>
---
 xdiff/xmerge.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/xdiff/xmerge.c b/xdiff/xmerge.c
index d98f430..f338ad6 100644
--- a/xdiff/xmerge.c
+++ b/xdiff/xmerge.c
@@ -641,8 +641,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 ||
@@ -654,6 +657,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;
 	}
-- 
2.7.1
Previous: Patrick Steinhardt
Message 4 of 4 in “xdiff/xmerge: fix memory leak in xdl_merge”
  1. xdiff/xmerge: fix memory leak in xdl_mergePatrick Steinhardt, Feb 23, 2016
  2. Johannes SchindelinFeb 23, 2016
  3. Patrick SteinhardtFeb 23, 2016
  4. xdiff/xmerge: fix memory leak in xdl_mergePatrick Steinhardt, Feb 23, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.