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

[PATCH 4/7] setup: make sure git dir path is in a permanent buffer

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Oct 2, 2010, 08:36 UTC
Message-ID
<20101002083652.GE29638@burratino>
In-Reply-To
<20101002082752.GA29638@burratino>

If setup_git_env() is run before the usual repository discovery sequence and .git is a file with the text

	gitdir: <path>

(with <path> any string) then the in-core git_dir variable is set to the result of converting <path> to an absolute path using make_absolute_path().

Unfortunately make_absolute_path() returns its result in a static buffer that is overwritten by later calls. Such a call could cause later accesses to git_dir (from git_pathdup(), for example) to read the wrong path, leaving git very confused.

It is not obvious whether any existing code in git will trigger the problem, but in any case, it is worth a few dozen bytes to copy the return value from make_absolute_path() for some added peace of mind.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 environment.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
diff --git a/environment.c b/environment.c
index c44a30b..de5581f 100644
--- a/environment.c
+++ b/environment.c
@@ -87,8 +87,10 @@ const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = {
 static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
-	if (!git_dir)
+	if (!git_dir) {
 		git_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
+		git_dir = git_dir ? xstrdup(git_dir) : NULL;
+	}
 	if (!git_dir)
 		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
 	git_object_dir = getenv(DB_ENVIRONMENT);
-- 
1.7.2.3
Previous: Jonathan NiederNext: Nguyen Thai Ngoc Duy
Message 21 of 33 in “memory leak reported by valgrind”
  1. memory leak reported by valgrindIvan Kanis, Aug 9, 2010
  2. Junio C HamanoAug 9, 2010
  3. Erik Faye-LundAug 9, 2010
  4. Alex RiesenAug 9, 2010
  5. Jonathan NiederAug 10, 2010
  6. 1/3 core: Stop leaking ondisk_cache_entrysJonathan Nieder, Aug 10, 2010
  7. 2/3 write-tree: Avoid leak when index refers to an invalid objectJonathan Nieder, Aug 10, 2010
  8. 3/3 read-tree: stop leaking tree objectsJonathan Nieder, Aug 10, 2010
  9. 0/7 Re: [PATCH] memory leak reported by valgrindJonathan Nieder, Oct 2, 2010
  10. 1/7 init: plug tiny one-time memory leakJonathan Nieder, Oct 2, 2010
  11. Junio C HamanoOct 3, 2010
  12. init: plug tiny one-time memory leakJonathan Nieder, Oct 4, 2010
  13. 2/7 lockfile: introduce alloc_lock_file() to avoid valgrind noiseJonathan Nieder, Oct 2, 2010
  14. Andreas EricssonOct 2, 2010
  15. Jonathan NiederOct 2, 2010
  16. Junio C HamanoOct 6, 2010
  17. Jonathan NiederOct 6, 2010
  18. Junio C HamanoOct 6, 2010
  19. Jonathan NiederOct 6, 2010
  20. 3/7 environment.c: remove unused variableJonathan Nieder, Oct 2, 2010
  21. 4/7 setup: make sure git dir path is in a permanent bufferJonathan Nieder, Oct 2, 2010
  22. Nguyen Thai Ngoc DuyOct 4, 2010
  23. 5/7 Introduce malloc/strdup/pathdup variants for permanent allocationsJonathan Nieder, Oct 2, 2010
  24. 6/7 environment: use alloc_permanent() for computed git_dir and coJonathan Nieder, Oct 2, 2010
  25. 7/7 commit-tree: free commit message before exitingJonathan Nieder, Oct 2, 2010
  26. Sverre RabbelierOct 2, 2010
  27. Jonathan NiederOct 2, 2010
  28. Sverre RabbelierOct 2, 2010
  29. Jonathan NiederOct 2, 2010
  30. Junio C HamanoOct 4, 2010
  31. Jonathan NiederOct 4, 2010
  32. Junio C HamanoOct 4, 2010
  33. Jonathan NiederOct 4, 2010

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.