From 18aa44d3b6d2ce2612a6db742e698e76c0dda6c3 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 16:16:58 +0100
Subject: [PATCH] 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 2047b03..7bccee6 100644
--- a/src/port/win32env.c
+++ b/src/port/win32env.c
@@ -73,15 +73,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
@@ -89,7 +84,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.2.windows.1

