git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jared Hance <jaredhance@gmail.com>
To: git@vger.kernel.org
Cc: Jared Hance <jaredhance@gmail.com>
Subject: [PATCH 2/3] Fix memory leak in apply_patch in apply.c.
Date: Fri,  2 Mar 2012 21:31:14 -0500	[thread overview]
Message-ID: <03cc2ba05a5744d8b48700556f1942757151ec4b.1330740964.git.jaredhance@gmail.com> (raw)
In-Reply-To: <cover.1330740964.git.jaredhance@gmail.com>
In-Reply-To: <cover.1330740964.git.jaredhance@gmail.com>

In the while loop inside apply_patch, patch is dynamically allocated
with a calloc. However, only unused patches are actually free'd; the
rest are left in a memory leak. Since a list is actively built up
consisting of the used patches, they can simply be iterated and free'd
at the end of the function.

Signed-off-by: Jared Hance <jaredhance@gmail.com>
---
 builtin/apply.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 389898f..92ebd57 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -3685,9 +3685,9 @@ static int apply_patch(int fd, const char *filename, int options)
 	size_t offset;
 	struct strbuf buf = STRBUF_INIT;
 	struct patch *list = NULL, **listp = &list;
+	struct patch *patch_iter;
 	int skipped_patch = 0;
 
-	/* FIXME - memory leak when using multiple patch files as inputs */
 	memset(&fn_table, 0, sizeof(struct string_list));
 	patch_input_file = filename;
 	read_patch_file(&buf, fd);
@@ -3712,7 +3712,6 @@ static int apply_patch(int fd, const char *filename, int options)
 			listp = &patch->next;
 		}
 		else {
-			/* perhaps free it a bit better? */
 			free(patch);
 			skipped_patch++;
 		}
@@ -3753,6 +3752,13 @@ static int apply_patch(int fd, const char *filename, int options)
 
 	if (summary)
 		summary_patch_list(list);
+	
+	patch_iter = list;
+	while(patch_iter != NULL) {
+	    struct patch *patch_iter_next = patch_iter->next;
+	    free(patch_iter);
+	    patch_iter = patch_iter_next;
+	}
 
 	strbuf_release(&buf);
 	return 0;
-- 
1.7.3.4

  parent reply	other threads:[~2012-03-03  2:31 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-03  2:31 [PATCH 0/3] Fix some documented fixmes Jared Hance
2012-03-03  2:31 ` [PATCH 1/3] Use startup_info->prefix rather than prefix Jared Hance
2012-03-03  7:30   ` Junio C Hamano
2012-03-03  9:50     ` Nguyen Thai Ngoc Duy
2012-03-03 21:44       ` Junio C Hamano
2012-03-03 23:23         ` Jared Hance
2012-03-03  2:31 ` Jared Hance [this message]
2012-03-03  7:41   ` [PATCH 2/3] Fix memory leak in apply_patch in apply.c Junio C Hamano
2012-03-03  2:31 ` [PATCH 3/3] Add threaded versions of functions in symlinks.c Jared Hance
2012-03-03  7:55   ` Junio C Hamano
2012-03-03 14:40 ` [PATCH v2 0/3] Fix a few documents fixmes Jared Hance
2012-03-03 14:40   ` [PATCH v2 1/3] Use startup_info->prefix rather than prefix Jared Hance
2012-03-03 14:47     ` Jeff Epler
2012-03-03 14:40   ` [PATCH v2 2/3] Fix memory leak in apply_patch in apply.c Jared Hance
2012-03-03 15:05     ` Jared Hance
2012-03-03 21:51     ` Junio C Hamano
2012-03-03 14:40   ` [PATCH v2 3/3] Add threaded versions of functions in symlinks.c Jared Hance
2012-03-05 11:00     ` Thomas Rast

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=03cc2ba05a5744d8b48700556f1942757151ec4b.1330740964.git.jaredhance@gmail.com \
    --to=jaredhance@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).