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

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

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Sep 20, 2017, 19:47 UTC
Message-ID
<1505936846-2195-4-git-send-email-martin.agren@gmail.com>

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
Next: Jeff King
Message 1 of 12 in “revision: fix memory leaks with `struct cmdline_pathspec`”
  1. revision: fix memory leaks with `struct cmdline_pathspec`Martin Ågren, Sep 20, 2017
  2. Jeff KingSep 20, 2017
  3. revision: replace "struct cmdline_pathspec" with argv_arrayJeff King, Sep 20, 2017
  4. Jonathan NiederSep 20, 2017
  5. Jeff KingSep 21, 2017
  6. Jonathan NiederSep 21, 2017
  7. Jeff KingSep 21, 2017
  8. Jonathan NiederSep 21, 2017
  9. Jeff KingSep 21, 2017
  10. Junio C HamanoSep 21, 2017
  11. Martin ÅgrenSep 21, 2017
  12. Junio C HamanoSep 21, 2017

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.