]> granicus.if.org Git - git/commitdiff
get_super_prefix(): copy getenv() result
authorJeff King <peff@peff.net>
Fri, 11 Jan 2019 22:15:00 +0000 (17:15 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sat, 12 Jan 2019 02:48:58 +0000 (18:48 -0800)
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>
environment.c

index 3f3c8746c2b6e30c5d9e40772078a39c3a9638c8..8fa10cc4310373683a087e539d4d494c6eff2279 100644 (file)
@@ -107,7 +107,7 @@ char *git_work_tree_cfg;
 
 static char *git_namespace;
 
-static const char *super_prefix;
+static char *super_prefix;
 
 /*
  * Repository-local GIT_* environment variables; see cache.h for details.
@@ -240,7 +240,7 @@ const char *get_super_prefix(void)
 {
        static int initialized;
        if (!initialized) {
-               super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
+               super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
                initialized = 1;
        }
        return super_prefix;