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

[PATCH 2/2] builtin/commit-graph.c: UNLEAK variables

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Oct 3, 2018, 15:36 UTC
Message-ID
<5d260588675f6446079efc05b00492f2c99948a3.1538579441.git.martin.agren@gmail.com>
In-Reply-To
<cover.1538579441.git.martin.agren@gmail.com>

`graph_verify()`, `graph_read()` and `graph_write()` do the hard work of `cmd_commit_graph()`. As soon as these return, so does `cmd_commit_graph()`.

`strbuf_getline()` may allocate memory in the strbuf, yet return EOF. We need to release the strbuf or UNLEAK it. Go for the latter since we are close to returning from `graph_write()`.

`graph_write()` also fails to free the strings in the string list. They have been added to the list with `strdup_strings` set to 0. We could flip `strdup_strings` before clearing the list, which is our usual hack in situations like this. But since we are about to exit, let's just UNLEAK the whole string list instead.

UNLEAK `graph` in `graph_verify`. While at it, and for consistency, UNLEAK in `graph_read()` as well, and remove an unnecessary UNLEAK just before dying.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 builtin/commit-graph.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index bc0fa9ba52..66f12eb009 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -64,6 +64,7 @@ static int graph_verify(int argc, const char **argv)
 	if (!graph)
 		return 0;
 
+	UNLEAK(graph);
 	return verify_commit_graph(the_repository, graph);
 }
 
@@ -89,10 +90,8 @@ static int graph_read(int argc, const char **argv)
 	graph_name = get_commit_graph_filename(opts.obj_dir);
 	graph = load_commit_graph_one(graph_name);
 
-	if (!graph) {
-		UNLEAK(graph_name);
+	if (!graph)
 		die("graph file %s does not exist", graph_name);
-	}
 
 	FREE_AND_NULL(graph_name);
 
@@ -115,7 +114,7 @@ static int graph_read(int argc, const char **argv)
 		printf(" large_edges");
 	printf("\n");
 
-	free_commit_graph(graph);
+	UNLEAK(graph);
 
 	return 0;
 }
@@ -166,6 +165,8 @@ static int graph_write(int argc, const char **argv)
 			pack_indexes = &lines;
 		if (opts.stdin_commits)
 			commit_hex = &lines;
+
+		UNLEAK(buf);
 	}
 
 	write_commit_graph(opts.obj_dir,
@@ -174,7 +175,7 @@ static int graph_write(int argc, const char **argv)
 			   opts.append,
 			   1);
 
-	string_list_clear(&lines, 0);
+	UNLEAK(lines);
 	return 0;
 }
 
-- 
2.19.0.329.g76f2f5c1e3
Previous: Martin ÅgrenNext: Derrick Stolee
Message 12 of 20 in “Clean up leaks in commit-graph.c”
  1. 0/2 Clean up leaks in commit-graph.cDerrick Stolee via GitGitGadget, Oct 2, 2018
  2. 1/2 commit-graph: clean up leaked memory during writeDerrick Stolee via GitGitGadget, Oct 2, 2018
  3. Martin ÅgrenOct 2, 2018
  4. Stefan BellerOct 2, 2018
  5. Martin ÅgrenOct 2, 2018
  6. Stefan BellerOct 2, 2018
  7. Jeff KingOct 2, 2018
  8. Stefan BellerOct 2, 2018
  9. Derrick StoleeOct 3, 2018
  10. 0/2 commit-graph: more leak fixesMartin Ågren, Oct 3, 2018
  11. 1/2 commit-graph: free `struct packed_git` after closing itMartin Ågren, Oct 3, 2018
  12. 2/2 builtin/commit-graph.c: UNLEAK variablesMartin Ågren, Oct 3, 2018
  13. Derrick StoleeOct 3, 2018
  14. Martin ÅgrenOct 3, 2018
  15. Jeff KingOct 2, 2018
  16. 2/2 commit-graph: reduce initial oid allocationDerrick Stolee via GitGitGadget, Oct 2, 2018
  17. 0/3 Clean up leaks in commit-graph.cDerrick Stolee via GitGitGadget, Oct 3, 2018
  18. 1/3 commit-graph: clean up leaked memory during writeDerrick Stolee via GitGitGadget, Oct 3, 2018
  19. 2/3 builtin/commit-graph.c: UNLEAK variablesMartin Ågren via GitGitGadget, Oct 3, 2018
  20. 3/3 commit-graph: reduce initial oid allocationDerrick Stolee via GitGitGadget, Oct 3, 2018

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.