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

[PATCH 09/10] set_git_dir: handle feeding gitdir to itself

From
Jeff King <peff@peff.net>
Date
Sep 5, 2017, 13:05 UTC
Message-ID
<20170905130501.xwc2ud2pwpbhqsli@sigill.intra.peff.net>
In-Reply-To
<20170905130149.agc3zp3s6i6e5aki@sigill.intra.peff.net>

Ideally we'd free the existing gitdir field before assigning the new one, to avoid a memory leak. But we can't do so safely because some callers do the equivalent of:

  set_git_dir(get_git_dir());

We can detect that case as a noop, but there are even more complicated cases like:

  set_git_dir(remove_leading_path(worktree, get_git_dir());

where we really do need to do some work, but the original string must remain valid.

Rather than put the burden on callers to make a copy of the string (only to free it later, since we'll make a copy of it ourselves), let's solve the problem inside set_git_dir(). We can make a copy of the pointer for the old gitdir, and then avoid freeing it until after we've made our new copy.

Signed-off-by: Jeff King <peff@peff.net>
---
 repository.c | 10 +++-------
 setup.c      |  5 -----
 2 files changed, 3 insertions(+), 12 deletions(-)
diff --git a/repository.c b/repository.c
index 52f1821c6b..97c732bd48 100644
--- a/repository.c
+++ b/repository.c
@@ -56,16 +56,12 @@ static void repo_setup_env(struct repository *repo)
 void repo_set_gitdir(struct repository *repo, const char *path)
 {
 	const char *gitfile = read_gitfile(path);
+	char *old_gitdir = repo->gitdir;
 
-	/*
-	 * NEEDSWORK: Eventually we want to be able to free gitdir and the rest
-	 * of the environment before reinitializing it again, but we have some
-	 * crazy code paths where we try to set gitdir with the current gitdir
-	 * and we don't want to free gitdir before copying the passed in value.
-	 */
 	repo->gitdir = xstrdup(gitfile ? gitfile : path);
-
 	repo_setup_env(repo);
+
+	free(old_gitdir);
 }
 
 /*
diff --git a/setup.c b/setup.c
index 23950173fc..6d8380acd2 100644
--- a/setup.c
+++ b/setup.c
@@ -399,11 +399,6 @@ void setup_work_tree(void)
 	if (getenv(GIT_WORK_TREE_ENVIRONMENT))
 		setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
 
-	/*
-	 * NEEDSWORK: this call can essentially be set_git_dir(get_git_dir())
-	 * which can cause some problems when trying to free the old value of
-	 * gitdir.
-	 */
 	set_git_dir(remove_leading_path(git_dir, work_tree));
 	initialized = 1;
 }
-- 
2.14.1.721.gc5bc1565f1
Previous: Jeff KingNext: Brandon Williams
Message 10 of 42 in “towards clean leak-checker output”
  1. 0/10 towards clean leak-checker outputJeff King, Sep 5, 2017
  2. 01/10 test-lib: --valgrind should not override --verbose-logJeff King, Sep 5, 2017
  3. 02/10 test-lib: set LSAN_OPTIONS to abort by defaultJeff King, Sep 5, 2017
  4. 03/10 add: free leaked pathspec after add_files_to_cache()Jeff King, Sep 5, 2017
  5. 04/10 update-index: fix cache entry leak in add_one_file()Jeff King, Sep 5, 2017
  6. 05/10 config: plug user_config leakJeff King, Sep 5, 2017
  7. 06/10 reset: make tree counting less confusingJeff King, Sep 5, 2017
  8. 07/10 reset: free allocated tree buffersJeff King, Sep 5, 2017
  9. 08/10 repository: free fields before overwriting themJeff King, Sep 5, 2017
  10. 09/10 set_git_dir: handle feeding gitdir to itselfJeff King, Sep 5, 2017
  11. Brandon WilliamsSep 7, 2017
  12. 10/10 add UNLEAK annotation for reducing leak false positivesJeff King, Sep 5, 2017
  13. Stefan BellerSep 5, 2017
  14. Jeff KingSep 7, 2017
  15. Stefan BellerSep 7, 2017
  16. Kaartic SivaraamSep 12, 2017
  17. Jeff KingSep 12, 2017
  18. Kaartic SivaraamSep 13, 2017
  19. Martin ÅgrenSep 6, 2017
  20. Jeff KingSep 7, 2017
  21. Kaartic SivaraamSep 12, 2017
  22. Jeff KingSep 12, 2017
  23. Kaartic SivaraamSep 13, 2017
  24. Martin ÅgrenSep 5, 2017
  25. Jeff KingSep 5, 2017
  26. Martin ÅgrenSep 5, 2017
  27. Jeff KingSep 6, 2017
  28. Junio C HamanoSep 6, 2017
  29. 0/2 simplifying !RUNTIME_PREFIXJeff King, Sep 6, 2017
  30. 1/2 system_path: move RUNTIME_PREFIX to a sub-functionJeff King, Sep 6, 2017
  31. Johannes SchindelinSep 6, 2017
  32. Jeff KingSep 6, 2017
  33. 2/2 git_extract_argv0_path: do nothing without RUNTIME_PREFIXJeff King, Sep 6, 2017
  34. 10/10 add UNLEAK annotation for reducing leak false positivesJeff King, Sep 8, 2017
  35. Jonathan TanSep 19, 2017
  36. Jeff KingSep 19, 2017
  37. git-compat-util: make UNLEAK less error-proneJonathan Tan, Sep 19, 2017
  38. Jeff KingSep 19, 2017
  39. git-compat-util: make UNLEAK less error-proneJonathan Tan, Sep 19, 2017
  40. Junio C HamanoSep 20, 2017
  41. Jeff KingSep 20, 2017
  42. Junio C HamanoSep 20, 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.