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

[PATCH] exec_cmd: system_path memory leak fix

From
0xAX <kuleshovmail@gmail.com>
Date
Nov 23, 2014, 13:56 UTC
Message-ID
<1416750981-24446-2-git-send-email-kuleshovmail@gmail.com>
In-Reply-To
<1416750981-24446-1-git-send-email-kuleshovmail@gmail.com>
Signed-off-by: 0xAX <kuleshovmail@gmail.com>
---
 exec_cmd.c | 25 ++++++++++++++++---------
 exec_cmd.h |  4 ++--
 git.c      | 12 +++++++++---
 3 files changed, 27 insertions(+), 14 deletions(-)
diff --git a/exec_cmd.c b/exec_cmd.c
index 698e752..08f8f80 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -6,7 +6,7 @@
 static const char *argv_exec_path;
 static const char *argv0_path;
 
-const char *system_path(const char *path)
+char *system_path(const char *path)
 {
 #ifdef RUNTIME_PREFIX
 	static const char *prefix;
@@ -14,9 +14,10 @@ const char *system_path(const char *path)
 	static const char *prefix = PREFIX;
 #endif
 	struct strbuf d = STRBUF_INIT;
+	char *new_path = NULL;
 
 	if (is_absolute_path(path))
-		return path;
+		return strdup(path);
 
 #ifdef RUNTIME_PREFIX
 	assert(argv0_path);
@@ -32,10 +33,13 @@ const char *system_path(const char *path)
 				"Using static fallback '%s'.\n", prefix);
 	}
 #endif
-
 	strbuf_addf(&d, "%s/%s", prefix, path);
-	path = strbuf_detach(&d, NULL);
-	return path;
+	new_path = malloc((strlen(prefix) + strlen(path)) + 2);
+	sprintf(new_path, "%s/%s", prefix, path);
+
+	strbuf_release(&d);
+
+	return new_path;
 }
 
 const char *git_extract_argv0_path(const char *argv0)
@@ -68,16 +72,16 @@ void git_set_argv_exec_path(const char *exec_path)
 
 
 /* Returns the highest-priority, location to look for git programs. */
-const char *git_exec_path(void)
+char *git_exec_path(void)
 {
 	const char *env;
 
 	if (argv_exec_path)
-		return argv_exec_path;
+		return strdup(argv_exec_path);
 
 	env = getenv(EXEC_PATH_ENVIRONMENT);
 	if (env && *env) {
-		return env;
+		return strdup(env);
 	}
 
 	return system_path(GIT_EXEC_PATH);
@@ -96,7 +100,9 @@ void setup_path(void)
 	const char *old_path = getenv("PATH");
 	struct strbuf new_path = STRBUF_INIT;
 
-	add_path(&new_path, git_exec_path());
+	char *exec_path = git_exec_path();
+
+	add_path(&new_path, exec_path);
 	add_path(&new_path, argv0_path);
 
 	if (old_path)
@@ -107,6 +113,7 @@ void setup_path(void)
 	setenv("PATH", new_path.buf, 1);
 
 	strbuf_release(&new_path);
+	free(exec_path);
 }
 
 const char **prepare_git_cmd(const char **argv)
diff --git a/exec_cmd.h b/exec_cmd.h
index e4c9702..03c8599 100644
--- a/exec_cmd.h
+++ b/exec_cmd.h
@@ -3,12 +3,12 @@
 
 extern void git_set_argv_exec_path(const char *exec_path);
 extern const char *git_extract_argv0_path(const char *path);
-extern const char *git_exec_path(void);
+extern char *git_exec_path(void);
 extern void setup_path(void);
 extern const char **prepare_git_cmd(const char **argv);
 extern int execv_git_cmd(const char **argv); /* NULL terminated */
 LAST_ARG_MUST_BE_NULL
 extern int execl_git_cmd(const char *cmd, ...);
-extern const char *system_path(const char *path);
+extern char *system_path(const char *path);
 
 #endif /* GIT_EXEC_CMD_H */
diff --git a/git.c b/git.c
index 82d7a1c..499dc2a 100644
--- a/git.c
+++ b/git.c
@@ -99,13 +99,19 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 				exit(0);
 			}
 		} else if (!strcmp(cmd, "--html-path")) {
-			puts(system_path(GIT_HTML_PATH));
+			char *git_html_path = system_path(GIT_HTML_PATH);
+			puts(git_html_path);
+			free(git_html_path);
 			exit(0);
 		} else if (!strcmp(cmd, "--man-path")) {
-			puts(system_path(GIT_MAN_PATH));
+			char *git_man_path = system_path(GIT_MAN_PATH);
+			puts(git_man_path);
+			free(git_man_path);
 			exit(0);
 		} else if (!strcmp(cmd, "--info-path")) {
-			puts(system_path(GIT_INFO_PATH));
+			char *git_info_path = system_path(GIT_INFO_PATH);
+			puts(git_info_path);
+			free(git_info_path);
 			exit(0);
 		} else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
 			use_pager = 1;
-- 
2.2.0.rc3.191.gfdea3f0
Previous: 0xAXNext: 0xAX
Message 2 of 33 in “GIT: [PATCH] exec_cmd: system_path memory leak fix”
  1. 0xAXNov 23, 2014
  2. exec_cmd: system_path memory leak fix0xAX, Nov 23, 2014
  3. 0xAXNov 23, 2014
  4. Junio C HamanoNov 23, 2014
  5. Alex KuleshovNov 23, 2014
  6. Alex KuleshovNov 23, 2014
  7. Jeff KingNov 23, 2014
  8. Eric SunshineNov 23, 2014
  9. Junio C HamanoNov 23, 2014
  10. Alex KuleshovNov 24, 2014
  11. Junio C HamanoNov 24, 2014
  12. Alex KuleshovNov 24, 2014
  13. Alexander KuleshovNov 24, 2014
  14. Alex KuleshovNov 24, 2014
  15. change contract between system_path and it's callers0xAX, Nov 24, 2014
  16. Re*: [PATCH] change contract between system_path and it's callersJunio C Hamano, Nov 24, 2014
  17. Alex KuleshovNov 24, 2014
  18. Junio C HamanoNov 24, 2014
  19. Junio C HamanoNov 24, 2014
  20. Alexander KuleshovNov 25, 2014
  21. Alexander KuleshovNov 25, 2014
  22. Junio C HamanoNov 25, 2014
  23. Alexander KuleshovNov 25, 2014
  24. 1/1 change contract between system_path and it's callersAlexander Kuleshov, Nov 25, 2014
  25. Junio C HamanoNov 25, 2014
  26. Alexander KuleshovNov 26, 2014
  27. Alexander KuleshovNov 26, 2014
  28. Alexander KuleshovNov 26, 2014
  29. Junio C HamanoNov 26, 2014
  30. Philip OakleyNov 28, 2014
  31. Junio C HamanoNov 25, 2014
  32. Alexander KuleshovNov 25, 2014
  33. Junio C HamanoNov 23, 2014

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.