From 27d47434e83d5d81b912a194624910c655729431 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Tue, 26 Apr 2016 15:45:00 +0200
Subject: [PATCH 2/3] Fix the load race in pgwin32_putenv() (and open the
 unload race).

Before, any CRT first loaded after the first call to pgwin32_putenv() would
be frozen out because the "not found" result was cached for the lifetime of
the process.

This fixes the "load" race and makes it much more likely that an "unload" race
happens instead, where a CRT is loaded, noticed and cached, then unloaded, and
then the next call to pgwin32_putenv() crashes.
---
 src/port/win32env.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/src/port/win32env.c b/src/port/win32env.c
index 77f8334..3f56ba8 100644
--- a/src/port/win32env.c
+++ b/src/port/win32env.c
@@ -82,15 +82,10 @@ pgwin32_putenv(const char *envval)
 		{
 			if (rtmodules[i].hmodule == NULL)
 			{
-				/* Not attempted before, so try to find this DLL */
+				/* Try to find this DLL */
 				rtmodules[i].hmodule = GetModuleHandle(rtmodules[i].modulename);
 				if (rtmodules[i].hmodule == NULL)
 				{
-					/*
-					 * Set to INVALID_HANDLE_VALUE so we know we have tried
-					 * this one before, and won't try again.
-					 */
-					rtmodules[i].hmodule = INVALID_HANDLE_VALUE;
 					continue;
 				}
 				else
@@ -98,7 +93,6 @@ pgwin32_putenv(const char *envval)
 					rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv");
 					if (rtmodules[i].putenvFunc == NULL)
 					{
-						rtmodules[i].hmodule = INVALID_HANDLE_VALUE;
 						continue;
 					}
 				}
-- 
2.10.0

