diff options
author | Karsten Blees <blees@dcon.de> | 2014-07-17 19:37:57 +0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-07-21 20:32:49 +0400 |
commit | e96942e821dec273b884fec378cc2a97a7f5d689 (patch) | |
tree | 86c3f3a1efa3bdf6989b819ad5409dc3402be71f /compat | |
parent | b729f98fa50b10cfba7cbf3f37a0ac255e1fbdcd (diff) |
Win32: fix environment memory leaks
All functions that modify the environment have memory leaks.
Disable gitunsetenv in the Makefile and use env_setenv (via mingw_putenv)
instead (this frees removed environment entries).
Move xstrdup from env_setenv to make_augmented_environ, so that
mingw_putenv no longer copies the environment entries (according to POSIX
[1], "the string [...] shall become part of the environment"). This also
fixes the memory leak in gitsetenv, which expects a POSIX compliant putenv.
[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/putenv.html
Note: This patch depends on taking control of char **environ and having
our own mingw_putenv (both introduced in "Win32: Unicode environment
(incoming)").
Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'compat')
-rw-r--r-- | compat/mingw.c | 10 | ||||
-rw-r--r-- | compat/mingw.h | 1 |
2 files changed, 7 insertions, 4 deletions
diff --git a/compat/mingw.c b/compat/mingw.c index cb0914af6c..f6c7ad7fd8 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1220,14 +1220,14 @@ static char **env_setenv(char **env, const char *name) for (i = 0; env[i]; i++) ; env = xrealloc(env, (i+2)*sizeof(*env)); - env[i] = xstrdup(name); + env[i] = (char*) name; env[i+1] = NULL; } } else { free(env[i]); if (*eq) - env[i] = xstrdup(name); + env[i] = (char*) name; else for (; env[i]; i++) env[i] = env[i+1]; @@ -1242,8 +1242,10 @@ char **make_augmented_environ(const char *const *vars) { char **env = copy_environ(); - while (*vars) - env = env_setenv(env, *vars++); + while (*vars) { + const char *v = *vars++; + env = env_setenv(env, strchr(v, '=') ? xstrdup(v) : v); + } return env; } diff --git a/compat/mingw.h b/compat/mingw.h index ca80be1bbe..828d97760c 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -209,6 +209,7 @@ char *mingw_getenv(const char *name); #define getenv mingw_getenv int mingw_putenv(const char *namevalue); #define putenv mingw_putenv +#define unsetenv mingw_putenv int mingw_gethostname(char *host, int namelen); #define gethostname mingw_gethostname |