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

[PATCH 11/14] lockfile API users: simplify and don't leak "path"

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Mar 2, 2022, 17:10 UTC
Message-ID
<patch-11.14-cc8beed10be-20220302T170718Z-avarab@gmail.com>
In-Reply-To
<cover-00.14-00000000000-20220302T170718Z-avarab@gmail.com>

Fix a memory leak in code added in 6c622f9f0bb (commit-graph: write commit-graph chains, 2019-06-18). We needed to free the "lock_name" if we encounter errors, and the "graph_name" after we'd run unlink() on it.

For the case of write_commit_graph_file() refactoring the code to free the "lock_name" after we were done using the "struct lock_file lk" would have made the control flow more complex. Luckily we can free the "lock_file" right after the hold_lock_file_for_update() call, if it makes use of "path" at all it'll have copied its contents to a "struct strbuf" of its own.

While I'm at it let's fix code added in fb10ca5b543 (sparse-checkout: write using lockfile, 2019-11-21) in write_patterns_and_update() to avoid the same complexity that I thought I needed when I wrote the initial fix for write_commit_graph_file(). We can free the "sparse_filename" right after calling hold_lock_file_for_update(), we don't need to wait until we're exiting the function to do so.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/sparse-checkout.c | 3 +--
 commit-graph.c            | 2 ++
 2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 9c338d33ea2..270ad49c2b8 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -328,11 +328,11 @@ static int write_patterns_and_update(struct pattern_list *pl)
 
 	fd = hold_lock_file_for_update(&lk, sparse_filename,
 				      LOCK_DIE_ON_ERROR);
+	free(sparse_filename);
 
 	result = update_working_directory(pl);
 	if (result) {
 		rollback_lock_file(&lk);
-		free(sparse_filename);
 		clear_pattern_list(pl);
 		update_working_directory(NULL);
 		return result;
@@ -348,7 +348,6 @@ static int write_patterns_and_update(struct pattern_list *pl)
 	fflush(fp);
 	commit_lock_file(&lk);
 
-	free(sparse_filename);
 	clear_pattern_list(pl);
 
 	return 0;
diff --git a/commit-graph.c b/commit-graph.c
index aab0b292774..b8cde7ea27d 100644
--- a/commit-graph.c
+++ b/commit-graph.c
@@ -1854,6 +1854,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 
 		hold_lock_file_for_update_mode(&lk, lock_name,
 					       LOCK_DIE_ON_ERROR, 0444);
+		free(lock_name);
 
 		fd = git_mkstemp_mode(ctx->graph_name, 0444);
 		if (fd < 0) {
@@ -1978,6 +1979,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
 		} else {
 			char *graph_name = get_commit_graph_filename(ctx->odb);
 			unlink(graph_name);
+			free(graph_name);
 		}
 
 		ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
-- 
2.35.1.1228.g56895c6ee86
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 16 of 37 in “tree-wide: small fixes for memory leaks”
  1. 00/14 tree-wide: small fixes for memory leaksÆvar Arnfjörð Bjarmason, Mar 2, 2022
  2. 01/14 index-pack: fix memory leaksÆvar Arnfjörð Bjarmason, Mar 2, 2022
  3. Derrick StoleeMar 2, 2022
  4. Ævar Arnfjörð BjarmasonMar 3, 2022
  5. 02/14 merge-base: free() allocated "struct commit **" listÆvar Arnfjörð Bjarmason, Mar 2, 2022
  6. 03/14 diff.c: free "buf" in diff_words_flush()Ævar Arnfjörð Bjarmason, Mar 2, 2022
  7. 05/14 remote-curl.c: free memory in cmd_main()Ævar Arnfjörð Bjarmason, Mar 2, 2022
  8. 04/14 urlmatch.c: add and use a *_release() functionÆvar Arnfjörð Bjarmason, Mar 2, 2022
  9. 06/14 bundle: call strvec_clear() on allocated strvecÆvar Arnfjörð Bjarmason, Mar 2, 2022
  10. 08/14 submodule--helper: fix trivial leak in module_add()Ævar Arnfjörð Bjarmason, Mar 2, 2022
  11. 07/14 transport: stop needlessly copying bundle header referencesÆvar Arnfjörð Bjarmason, Mar 2, 2022
  12. Derrick StoleeMar 2, 2022
  13. Ævar Arnfjörð BjarmasonMar 3, 2022
  14. 09/14 commit-graph: fix memory leak in misused string_list APIÆvar Arnfjörð Bjarmason, Mar 2, 2022
  15. 10/14 commit-graph: stop fill_oids_from_packs() progress on error and free()Ævar Arnfjörð Bjarmason, Mar 2, 2022
  16. 11/14 lockfile API users: simplify and don't leak "path"Ævar Arnfjörð Bjarmason, Mar 2, 2022
  17. 12/14 range-diff: plug memory leak in common invocationÆvar Arnfjörð Bjarmason, Mar 2, 2022
  18. 14/14 repository.c: free the "path cache" in repo_clear()Ævar Arnfjörð Bjarmason, Mar 2, 2022
  19. 13/14 range-diff: plug memory leak in read_patches()Ævar Arnfjörð Bjarmason, Mar 2, 2022
  20. Derrick StoleeMar 2, 2022
  21. 00/14 tree-wide: small fixes for memory leaksÆvar Arnfjörð Bjarmason, Mar 4, 2022
  22. 01/14 index-pack: fix memory leaksÆvar Arnfjörð Bjarmason, Mar 4, 2022
  23. 02/14 merge-base: free() allocated "struct commit **" listÆvar Arnfjörð Bjarmason, Mar 4, 2022
  24. 03/14 diff.c: free "buf" in diff_words_flush()Ævar Arnfjörð Bjarmason, Mar 4, 2022
  25. 04/14 urlmatch.c: add and use a *_release() functionÆvar Arnfjörð Bjarmason, Mar 4, 2022
  26. 05/14 remote-curl.c: free memory in cmd_main()Ævar Arnfjörð Bjarmason, Mar 4, 2022
  27. 06/14 bundle: call strvec_clear() on allocated strvecÆvar Arnfjörð Bjarmason, Mar 4, 2022
  28. 07/14 transport: stop needlessly copying bundle header referencesÆvar Arnfjörð Bjarmason, Mar 4, 2022
  29. Derrick StoleeMar 4, 2022
  30. 09/14 commit-graph: fix memory leak in misused string_list APIÆvar Arnfjörð Bjarmason, Mar 4, 2022
  31. 12/14 range-diff: plug memory leak in common invocationÆvar Arnfjörð Bjarmason, Mar 4, 2022
  32. 13/14 range-diff: plug memory leak in read_patches()Ævar Arnfjörð Bjarmason, Mar 4, 2022
  33. 11/14 lockfile API users: simplify and don't leak "path"Ævar Arnfjörð Bjarmason, Mar 4, 2022
  34. 14/14 repository.c: free the "path cache" in repo_clear()Ævar Arnfjörð Bjarmason, Mar 4, 2022
  35. 10/14 commit-graph: stop fill_oids_from_packs() progress on error and free()Ævar Arnfjörð Bjarmason, Mar 4, 2022
  36. 08/14 submodule--helper: fix trivial leak in module_add()Ævar Arnfjörð Bjarmason, Mar 4, 2022
  37. Derrick StoleeMar 4, 2022

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.