Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 10e1987

Browse files
kbleeskasal
authored andcommitted
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 <[email protected]>
1 parent 50cd615 commit 10e1987

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

compat/mingw.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,14 +1239,14 @@ static char **env_setenv(char **env, const char *name)
12391239
for (i = 0; env[i]; i++)
12401240
;
12411241
env = xrealloc(env, (i+2)*sizeof(*env));
1242-
env[i] = xstrdup(name);
1242+
env[i] = (char*) name;
12431243
env[i+1] = NULL;
12441244
}
12451245
}
12461246
else {
12471247
free(env[i]);
12481248
if (*eq)
1249-
env[i] = xstrdup(name);
1249+
env[i] = (char*) name;
12501250
else
12511251
for (; env[i]; i++)
12521252
env[i] = env[i+1];
@@ -1261,8 +1261,10 @@ char **make_augmented_environ(const char *const *vars)
12611261
{
12621262
char **env = copy_environ();
12631263

1264-
while (*vars)
1265-
env = env_setenv(env, *vars++);
1264+
while (*vars) {
1265+
const char *v = *vars++;
1266+
env = env_setenv(env, strchr(v, '=') ? xstrdup(v) : v);
1267+
}
12661268
return env;
12671269
}
12681270

compat/mingw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ char *mingw_getenv(const char *name);
209209
#define getenv mingw_getenv
210210
int mingw_putenv(const char *namevalue);
211211
#define putenv mingw_putenv
212+
#define unsetenv mingw_putenv
212213

213214
int mingw_gethostname(char *host, int namelen);
214215
#define gethostname mingw_gethostname

config.mak.uname

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ ifeq ($(uname_S),Windows)
321321
NO_IPV6 = YesPlease
322322
NO_UNIX_SOCKETS = YesPlease
323323
NO_SETENV = YesPlease
324-
NO_UNSETENV = YesPlease
325324
NO_STRCASESTR = YesPlease
326325
NO_STRLCPY = YesPlease
327326
NO_MEMMEM = YesPlease
@@ -473,7 +472,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
473472
NO_SYMLINK_HEAD = YesPlease
474473
NO_UNIX_SOCKETS = YesPlease
475474
NO_SETENV = YesPlease
476-
NO_UNSETENV = YesPlease
477475
NO_STRCASESTR = YesPlease
478476
NO_STRLCPY = YesPlease
479477
NO_MEMMEM = YesPlease

0 commit comments

Comments
 (0)