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

[PATCH v2 4/6] clone: fix a memory leak of the "git_dir" variable

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Oct 22, 2021, 08:55 UTC
Message-ID
<patch-v2-4.6-0d0e6359cf4-20211022T085306Z-avarab@gmail.com>
In-Reply-To
<cover-v2-0.6-00000000000-20211022T085306Z-avarab@gmail.com>

At this point in cmd_clone the "git_dir" is always either an xstrdup()'d string, or something we got from mkpathdup(). Let's free() it before we clobber it.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/clone.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index 559acf9e036..fb377b27657 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -1040,8 +1040,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
 		INIT_DB_QUIET);
 
-	if (real_git_dir)
+	if (real_git_dir) {
+		free((char *)git_dir);
 		git_dir = real_git_dir;
+	}
 
 	/*
 	 * additional config can be injected with -c, make sure it's included
-- 
2.33.1.1494.g88b39a443e1
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 18 of 20 in “leaks: miscellaneous small leak fixes”
  1. 0/6 leaks: miscellaneous small leak fixesÆvar Arnfjörð Bjarmason, Oct 21, 2021
  2. 1/6 grep: prefer "struct grep_opt" over its "void *"Ævar Arnfjörð Bjarmason, Oct 21, 2021
  3. Junio C HamanoOct 21, 2021
  4. 2/6 grep: use object_array_clear() in cmd_grep()Ævar Arnfjörð Bjarmason, Oct 21, 2021
  5. Junio C HamanoOct 21, 2021
  6. Junio C HamanoOct 21, 2021
  7. 3/6 clone: fix a memory leak of the "git_dir" variableÆvar Arnfjörð Bjarmason, Oct 21, 2021
  8. Junio C HamanoOct 22, 2021
  9. 4/6 submodule--helper: fix small memory leaksÆvar Arnfjörð Bjarmason, Oct 21, 2021
  10. 5/6 reflog: free() ref given to us by dwim_log()Ævar Arnfjörð Bjarmason, Oct 21, 2021
  11. Junio C HamanoOct 22, 2021
  12. 6/6 repack: stop leaking a "struct child_process"Ævar Arnfjörð Bjarmason, Oct 21, 2021
  13. Junio C HamanoOct 22, 2021
  14. 0/6 leaks: miscellaneous small leak fixesÆvar Arnfjörð Bjarmason, Oct 22, 2021
  15. 1/6 grep: prefer "struct grep_opt" over its "void *" equivalentÆvar Arnfjörð Bjarmason, Oct 22, 2021
  16. 2/6 grep: use object_array_clear() in cmd_grep()Ævar Arnfjörð Bjarmason, Oct 22, 2021
  17. 3/6 grep: fix a "path_list" memory leakÆvar Arnfjörð Bjarmason, Oct 22, 2021
  18. 4/6 clone: fix a memory leak of the "git_dir" variableÆvar Arnfjörð Bjarmason, Oct 22, 2021
  19. 5/6 submodule--helper: fix small memory leaksÆvar Arnfjörð Bjarmason, Oct 22, 2021
  20. 6/6 reflog: free() ref given to us by dwim_log()Ævar Arnfjörð Bjarmason, Oct 22, 2021

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.