get_super_prefix(): copy getenv() result

The return value of getenv() is not guaranteed to remain valid across multiple calls (nor across calls to setenv()). Since this function caches the result for the length of the program, we must make a copy to ensure that it is still valid when we need it. Reported-by: Yngve N. Pettersen <yngve@vivaldi.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 11, 2019 at 17:15 UTC 8aac69038fa6c5f957559ca7e08a5e2e8f74d0fa
1 file changed +2 -2
environment.c
+2 -2
@@ -107,7 +107,7 @@ char *git_work_tree_cfg;
107
108 static char *git_namespace;
109
110 -static const char *super_prefix;
110 +static char *super_prefix;
111
112 /*
113 * Repository-local GIT_* environment variables; see cache.h for details.
@@ -240,7 +240,7 @@ const char *get_super_prefix(void)
240 {
241 static int initialized;
242 if (!initialized) {
243 - super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
243 + super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
244 initialized = 1;
245 }
246 return super_prefix;