[PATCH 04/13] Win32: fix environment memory leaks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Karsten Blees <blees@xxxxxxx>

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@xxxxxxx>
Signed-off-by: Stepan Kasal <kasal@xxxxxx>
---
 compat/mingw.c   | 10 ++++++----
 compat/mingw.h   |  1 +
 config.mak.uname |  2 --
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index eadba8a..47e866c 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1219,14 +1219,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];
@@ -1241,8 +1241,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 c3889ca..ef94194 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -212,6 +212,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
diff --git a/config.mak.uname b/config.mak.uname
index 00cf4c6..15ee15e 100644
--- a/config.mak.uname
+++ b/config.mak.uname
@@ -327,7 +327,6 @@ ifeq ($(uname_S),Windows)
 	NO_IPV6 = YesPlease
 	NO_UNIX_SOCKETS = YesPlease
 	NO_SETENV = YesPlease
-	NO_UNSETENV = YesPlease
 	NO_STRCASESTR = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_MEMMEM = YesPlease
@@ -480,7 +479,6 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_SYMLINK_HEAD = YesPlease
 	NO_UNIX_SOCKETS = YesPlease
 	NO_SETENV = YesPlease
-	NO_UNSETENV = YesPlease
 	NO_STRCASESTR = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_MEMMEM = YesPlease
-- 
2.0.0.9635.g0be03cb

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]