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

[PATCH] system_path: use a static buffer

From
Carlos Martín Nieto <cmn@elego.de>
Date
Mar 17, 2011, 11:01 UTC
Message-ID
<1300359664-6230-1-git-send-email-cmn@elego.de>
In-Reply-To
<7vipvi7q5g.fsf@alter.siamese.dyndns.org>

Make system_path behave like the other path functions by using a static buffer, fixing a memory leak.

Also make sure the prefix pointer is always initialized to either PREFIX or NULL.

git_etc_gitattributes and git_etc_gitconfig are the only users who are affected by this change. Make them use a static buffer, which fits their use better as well.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---

On mié, 2011-03-16 at 13:43 -0700, Junio C Hamano wrote: Carlos Martín Nieto <cmn@elego.de> writes:

Show 16 quoted lines
> 
> > Make system_path behave like the other path functions by using a
> > static buffer, fixing a memory leak.
> >
> > Also make sure the prefix pointer is always initialized to either
> > PREFIX or NULL.
> >
> > Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
> > ---
> 
> Have you made sure all the callers are Ok with this change?
> 
> If somebody called system_path(GIT_EXEC_PATH), saved the result in a
> variable without copying, and then called system_path(ETC_GITATTRIBUTES),
> his variable may now have a value unrelated to GIT_EXEC_PATH, and you
> would fix all such callers to save the value away with strdup().

I checked again, and except for the ones changed in this patch, the rest copy it to their own buffer or pass it to puts, setenv or strbuf_addstr.

The way these functions are used suggest the caller expects them to deal with their own memory, so that's what I've done.

TBH, valgrind only reports a win of ~6-7kB when doing git log on git.git, but it's a step in the right direction (and adds consistency to system_path, which is the main win).

 attr.c     |    6 +++---
 config.c   |    6 +++---
 exec_cmd.c |   11 ++++++-----
 3 files changed, 12 insertions(+), 11 deletions(-)
diff --git a/attr.c b/attr.c
index 6aff695..64d803f 100644
--- a/attr.c
+++ b/attr.c
@@ -467,9 +467,9 @@ static void drop_attr_stack(void)
 
 const char *git_etc_gitattributes(void)
 {
-	static const char *system_wide;
-	if (!system_wide)
-		system_wide = system_path(ETC_GITATTRIBUTES);
+	static char system_wide[PATH_MAX];
+	if (!system_wide[0])
+		strlcpy(system_wide, system_path(ETC_GITATTRIBUTES), PATH_MAX);
 	return system_wide;
 }
 
diff --git a/config.c b/config.c
index 822ef83..cd1c295 100644
--- a/config.c
+++ b/config.c
@@ -808,9 +808,9 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
 
 const char *git_etc_gitconfig(void)
 {
-	static const char *system_wide;
-	if (!system_wide)
-		system_wide = system_path(ETC_GITCONFIG);
+	static char system_wide[PATH_MAX];
+	if (!system_wide[0])
+		strlcpy(system_wide, system_path(ETC_GITCONFIG), PATH_MAX);
 	return system_wide;
 }
 
diff --git a/exec_cmd.c b/exec_cmd.c
index 38545e8..5686952 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -9,11 +9,11 @@ static const char *argv0_path;
 const char *system_path(const char *path)
 {
 #ifdef RUNTIME_PREFIX
-	static const char *prefix;
+	static const char *prefix = NULL;
 #else
 	static const char *prefix = PREFIX;
 #endif
-	struct strbuf d = STRBUF_INIT;
+	static char buf[PATH_MAX];
 
 	if (is_absolute_path(path))
 		return path;
@@ -33,9 +33,10 @@ const char *system_path(const char *path)
 	}
 #endif
 
-	strbuf_addf(&d, "%s/%s", prefix, path);
-	path = strbuf_detach(&d, NULL);
-	return path;
+	if (snprintf(buf, sizeof(buf), "%s/%s", prefix, path) >= sizeof(buf))
+		die("system path too long for %s", path);
+
+	return buf;
 }
 
 const char *git_extract_argv0_path(const char *argv0)
-- 
1.7.4.1
Previous: Junio C HamanoNext: Carlos Martín Nieto
Message 24 of 48 in “Fix some errors reported by valgrind”
  1. 0/3 Fix some errors reported by valgrindCarlos Martín Nieto, Mar 14, 2011
  2. 1/3 make_absolute_path: Don't try to copy a string to itselfCarlos Martín Nieto, Mar 14, 2011
  3. Jeff KingMar 14, 2011
  4. Junio C HamanoMar 14, 2011
  5. Carlos Martín NietoMar 14, 2011
  6. Junio C HamanoMar 14, 2011
  7. Carlos Martín NietoMar 15, 2011
  8. Carlos Martín NietoMar 15, 2011
  9. Junio C HamanoMar 15, 2011
  10. Carlos Martín NietoMar 15, 2011
  11. Nguyen Thai Ngoc DuyMar 16, 2011
  12. Carlos Martín NietoMar 16, 2011
  13. Nguyen Thai Ngoc DuyMar 16, 2011
  14. Nguyen Thai Ngoc DuyMar 16, 2011
  15. Carlos Martín NietoMar 16, 2011
  16. 2/3 setup_path(): Free temporary bufferCarlos Martín Nieto, Mar 14, 2011
  17. Jeff KingMar 14, 2011
  18. Carlos Martín NietoMar 14, 2011
  19. system_path: use a static bufferCarlos Martín Nieto, Mar 16, 2011
  20. Erik Faye-LundMar 16, 2011
  21. Carlos Martín NietoMar 16, 2011
  22. system_path: use a static bufferCarlos Martín Nieto, Mar 16, 2011
  23. Junio C HamanoMar 16, 2011
  24. system_path: use a static bufferCarlos Martín Nieto, Mar 17, 2011
  25. system_path: use a static bufferCarlos Martín Nieto, Mar 17, 2011
  26. Junio C HamanoMar 18, 2011
  27. Carlos Martín NietoMar 21, 2011
  28. Jeff KingMar 21, 2011
  29. Carlos Martín NietoMar 21, 2011
  30. Jeff KingMar 21, 2011
  31. Carlos Martín NietoMar 21, 2011
  32. Nguyen Thai Ngoc DuyMar 18, 2011
  33. PATH_MAX (Re: [PATCH] system_path: use a static buffer)Jonathan Nieder, Mar 18, 2011
  34. Nguyen Thai Ngoc DuyMar 18, 2011
  35. Carlos Martín NietoMar 21, 2011
  36. Lasse MakholmMar 21, 2011
  37. Nguyen Thai Ngoc DuyMar 21, 2011
  38. 1/2 wrapper.c: add xgetcwd()Nguyễn Thái Ngọc Duy, Mar 18, 2011
  39. 2/2 setup_gently: use xgetcwd()Nguyễn Thái Ngọc Duy, Mar 18, 2011
  40. Junio C HamanoMar 14, 2011
  41. Carlos Martín NietoMar 14, 2011
  42. Jeff KingMar 15, 2011
  43. t/README: Add a note about running commands under valgrindCarlos Martín Nieto, Mar 15, 2011
  44. Junio C HamanoMar 15, 2011
  45. Carlos Martín NietoMar 15, 2011
  46. 3/3 clone: Free a few pathsCarlos Martín Nieto, Mar 14, 2011
  47. Jonathan NiederMar 14, 2011
  48. Junio C HamanoMar 18, 2011

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.