git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Martin Ågren" <martin.agren@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] revision: fix memory leaks with `struct cmdline_pathspec`
Date: Wed, 20 Sep 2017 21:47:26 +0200	[thread overview]
Message-ID: <1505936846-2195-4-git-send-email-martin.agren@gmail.com> (raw)

We don't free the array `prune_data.path` or the individual strings it
points to. Do so by introducing and using `free_cmdline_pathspec()`. To
be able to safely free the strings, always use `xstrdup()` when
assigning them. That does mean we allocate more memory than we used to,
but it also means it is clear who owns the strings and that we can stop
leaking those that we do allocate.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 revision.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/revision.c b/revision.c
index f9a90d7..dfb6a6c 100644
--- a/revision.c
+++ b/revision.c
@@ -1682,7 +1682,7 @@ static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
 {
 	while (*av) {
 		ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
-		prune->path[prune->nr++] = *(av++);
+		prune->path[prune->nr++] = xstrdup(*(av++));
 	}
 }
 
@@ -1695,6 +1695,18 @@ static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
 	}
 }
 
+static void free_cmdline_pathspec(struct cmdline_pathspec *prune)
+{
+	int i;
+
+	for (i = 0; i < prune->nr; i++)
+		free((void *)prune->path[i]);
+
+	FREE_AND_NULL(prune->path);
+	prune->nr = 0;
+	prune->alloc = 0;
+}
+
 static void read_revisions_from_stdin(struct rev_info *revs,
 				      struct cmdline_pathspec *prune)
 {
@@ -2392,6 +2404,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 		prune_data.path[prune_data.nr++] = NULL;
 		parse_pathspec(&revs->prune_data, 0, 0,
 			       revs->prefix, prune_data.path);
+		free_cmdline_pathspec(&prune_data);
 	}
 
 	if (revs->def == NULL)
-- 
2.14.1


             reply	other threads:[~2017-09-20 19:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-20 19:47 Martin Ågren [this message]
2017-09-20 20:25 ` [PATCH] revision: fix memory leaks with `struct cmdline_pathspec` Jeff King
2017-09-20 20:36   ` [PATCH] revision: replace "struct cmdline_pathspec" with argv_array Jeff King
2017-09-20 22:48     ` Jonathan Nieder
2017-09-21  3:04       ` Jeff King
2017-09-21  3:49         ` Jonathan Nieder
2017-09-21  4:48           ` Jeff King
2017-09-21  4:41         ` Jonathan Nieder
2017-09-21  4:50           ` Jeff King
2017-09-21  5:10           ` Junio C Hamano
2017-09-21  3:57     ` Martin Ågren
2017-09-21  4:11     ` Junio C Hamano

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=1505936846-2195-4-git-send-email-martin.agren@gmail.com \
    --to=martin.agren@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).