win32env.c bug with no msvcrt

Started by Magnus Haganderabout 16 years ago1 messages
#1Magnus Hagander
magnus@hagander.net
1 attachment(s)

There is code in src/port/win32env.c to update the environment within
the msvcrt.dll runtime even when running in msvcr70.dll (visual C++
2005). It updates that one, the local one *and* the system
environment.

However, in case there is no msvcrt.dll linked into the process, it
will abort at that stage and *not* update the system or local
environment. This happens during my tests on Visual Studio 2008, and
it causes the regression tests to fail (since they set environment
variables for subprocesses - thus needing the system env update)

The attached patch changes this to just ignore failure to find
msvcr.dll. It usually doesn't happen, because we almost always link
with third party libs that bring it in, but it can happen. Also, since
we *never* check the return code from putenv anyway, returning an
error code makes no sense...

Given that we've had zero reports of this on previous versions, I'm
not planning to backpatch this.

Comments?

--
Magnus Hagander
Me: http://www.hagander.net/
Work: http://www.redpill-linpro.com/

Attachments:

win32env.patchapplication/octet-stream; name=win32env.patchDownload
diff --git a/src/port/win32env.c b/src/port/win32env.c
index a2595a8..e5c0f0d 100644
--- a/src/port/win32env.c
+++ b/src/port/win32env.c
@@ -40,15 +40,20 @@ pgwin32_putenv(const char *envval)
 	if (putenvFunc == NULL)
 	{
 		hmodule = GetModuleHandle("msvcrt");
-		if (hmodule == NULL)
-			return 1;
-		putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv");
-		if (putenvFunc == NULL)
-			return 1;
+		if (hmodule != NULL)
+		{
+			/*
+			 * If the module is found, attempt to find the function. If not, that just
+			 * means we're not linked with msvcrt, so fall through and make our other
+			 * modifications anyway.
+			 * Ignore any errors and update whatever we can, since callers don't
+			 * check the return value anyway.
+			 */
+			putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv");
+			if (putenvFunc != NULL)
+				putenvFunc(envval);
+		}
 	}
-	ret = putenvFunc(envval);
-	if (ret != 0)
-		return ret;
 #endif   /* _MSC_VER >= 1300 */