[PATCH] revision: fix memory leaks with `struct cmdline_pathspec`

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux