Skip to content

Commit 007ac54

Browse files
peffgitster
authored andcommitted
git_exec_path: do not return the result of getenv()
The result of getenv() is not guaranteed by POSIX to last beyond another call to getenv(), or setenv(), etc. We should duplicate the string before returning to the caller to avoid any surprises. We already keep a cached pointer to avoid repeatedly leaking the result of system_path(). We can use the same pointer here to avoid allocating and leaking for each call. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c9bb5d1 commit 007ac54

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

exec_cmd.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,19 @@ void git_set_argv_exec_path(const char *exec_path)
6868
/* Returns the highest-priority, location to look for git programs. */
6969
const char *git_exec_path(void)
7070
{
71-
const char *env;
72-
static char *system_exec_path;
71+
static char *cached_exec_path;
7372

7473
if (argv_exec_path)
7574
return argv_exec_path;
7675

77-
env = getenv(EXEC_PATH_ENVIRONMENT);
78-
if (env && *env) {
79-
return env;
76+
if (!cached_exec_path) {
77+
const char *env = getenv(EXEC_PATH_ENVIRONMENT);
78+
if (env && *env)
79+
cached_exec_path = xstrdup(env);
80+
else
81+
cached_exec_path = system_path(GIT_EXEC_PATH);
8082
}
81-
82-
if (!system_exec_path)
83-
system_exec_path = system_path(GIT_EXEC_PATH);
84-
return system_exec_path;
83+
return cached_exec_path;
8584
}
8685

8786
static void add_path(struct strbuf *out, const char *path)

0 commit comments

Comments
 (0)