From: Christian Ullrich <chris@chrullrich.net>
To: Noah Misch <noah@leadboat.com>
Cc: Andrew Dunstan <andrew@dunslane.net>
Cc: Magnus Hagander <magnus@hagander.net>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: michael.paquier@gmail.com <michael.paquier@gmail.com>
Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
Date: Wed, 16 Nov 2016 20:45:20 +0000
Message-ID: <DB6PR0602MB2981431251C29DBAB293A6CAD4BE0@DB6PR0602MB2981.eurprd06.prod.outlook.com> (raw)
In-Reply-To: <20161116072246.GA2297058@tornado.leadboat.com>
References: <E1atXIK-0003un-G0@gemulon.postgresql.org>
<b0c66170-5711-20dd-2de3-408429ca0f48@chrullrich.net>
<CABUevEzicpYAUOF9ZHkee1-c-gvokKAmr708sqta4H71bOZOWQ@mail.gmail.com>
<34dd7956-3324-0305-288d-edc7d56fba76@chrullrich.net>
<571E6D90.7050903@dunslane.net>
<9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net>
<2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net>
<784524b2-bd50-1f5e-dad9-f8840f97753a@chrullrich.net>
<20161116072246.GA2297058@tornado.leadboat.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>
* Noah Misch wrote:
> On Tue, Apr 26, 2016 at 07:39:29PM +0200, Christian Ullrich
> wrote:
> > * Christian Ullrich wrote:
> Patch 1 looks good, except that it should be three patches:
>
> - cosmetic parts: change whitespace and s/0/NULL/
> - remove CloseHandle() call
> - probe for debug CRT modules, not just release CRT modules
Attached as 0001, 0002, 0003, in that order.
0004 is what used to be 0002, it disables caching of "DLL not
loaded" results.
> I recommend also moving the SetEnvironmentVariable() call before
> the putenv calls. That way, if a CRT loads while pgwin32_putenv()
> is executing, the newly-loaded CRT will always have the latest
> value.
Agreed, attached as 0005.
0006 was previously known as 0004, removing all caching.
> > Even with patch 0004, there is still a race condition between
> > the main thread and a theoretical additional thread created by
> > some other component that unloads some CRT between
> > GetProcAddress() and the _putenv() call, but that is hardly
> > fixable.
>
> I think you can fix it by abandoning GetModuleHandle() in favor
> of GetModuleHandleEx() + GetProcessAddress() + FreeLibrary().
Attached as 0007.
> > There is another possible fix, ugly as sin, if we really need
> > to keep the whole environment update machinery *and* cannot do
> > the full loop each time. Patch 0003 pins each CRT when we see
> > it for the first time.
This is now 0008.
Patch topology: master --- 1 .. 5 --- 6 --- 7
\
`- 8
> I prefer the simplicity of abandoning the cache (patch 4), if it
> performs decently. Would you compare the performance of patch 1,
> patches 1+2+3, and patches 1+2+4? This should measure the right
> thing (after substituting @libdir@ for your environment):
Tested with release builds; Core i7-6700K (quad/HT; 4000 MHz).
I did three runs each, and they were always within 0.5 % of each
other's run time.
- patch 1 (now 1-3): 24 μs/iteration (24 s for 1e6)
- patch 1+2+3 (now 1-5 + 8): 29 μs/iteration
- patch 1+2+4 (now 1-7): 30 μs/iteration
I also did a debug build with 1+2+4 that came in at 84 μs/iteration.
--
Christian
... now how do I get all the dangling debris out of the repo ...
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackersAttachments:download all (8 files, .tar.gz)0008-getmodulehandleex-pin.patch (application/octet-stream, 3.2K) download
From f7013462ccc10781bf1ac722d6f0f8c5b7c31b7b Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Tue, 6 Sep 2016 10:02:06 +0200
Subject: [PATCH] Pin any DLL as soon as seen when looking for _putenv().
---
src/port/win32env.c | 56 ++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 38 insertions(+), 18 deletions(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex d6391b0..48c4d82 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -75,26 +75,27 @@ pgwin32_putenv(const char *envval)
char *modulename;
HMODULE hmodule;
PUTENVPROC putenvFunc;
+ bool pinned;
} rtmodules[] =
{
- { "msvcrt", NULL, NULL },- { "msvcrtd", NULL, NULL }, /* Visual Studio 6.0 / mingw */- { "msvcr70", NULL, NULL },- { "msvcr70d", NULL, NULL }, /* Visual Studio 2002 */- { "msvcr71", NULL, NULL },- { "msvcr71d", NULL, NULL }, /* Visual Studio 2003 */- { "msvcr80", NULL, NULL },- { "msvcr80d", NULL, NULL }, /* Visual Studio 2005 */- { "msvcr90", NULL, NULL },- { "msvcr90d", NULL, NULL }, /* Visual Studio 2008 */- { "msvcr100", NULL, NULL },- { "msvcr100d", NULL, NULL }, /* Visual Studio 2010 */- { "msvcr110", NULL, NULL },- { "msvcr110d", NULL, NULL }, /* Visual Studio 2012 */- { "msvcr120", NULL, NULL },- { "msvcr120d", NULL, NULL }, /* Visual Studio 2013 */- { "ucrtbase", NULL, NULL },- { "ucrtbased", NULL, NULL }, /* Visual Studio 2015 and later */+ { "msvcrt", NULL, NULL, false },+ { "msvcrtd", NULL, NULL, false }, /* Visual Studio 6.0 / mingw */+ { "msvcr70", NULL, NULL, false },+ { "msvcr70d", NULL, NULL, false }, /* Visual Studio 2002 */+ { "msvcr71", NULL, NULL, false },+ { "msvcr71d", NULL, NULL, false }, /* Visual Studio 2003 */+ { "msvcr80", NULL, NULL, false },+ { "msvcr80d", NULL, NULL, false }, /* Visual Studio 2005 */+ { "msvcr90", NULL, NULL, false },+ { "msvcr90d", NULL, NULL, false }, /* Visual Studio 2008 */+ { "msvcr100", NULL, NULL, false },+ { "msvcr100d", NULL, NULL, false }, /* Visual Studio 2010 */+ { "msvcr110", NULL, NULL, false },+ { "msvcr110d", NULL, NULL, false }, /* Visual Studio 2012 */+ { "msvcr120", NULL, NULL, false },+ { "msvcr120d", NULL, NULL, false }, /* Visual Studio 2013 */+ { "ucrtbase", NULL, NULL, false },+ { "ucrtbased", NULL, NULL, false }, /* Visual Studio 2015 and later */
{ NULL, NULL, NULL }
};
int i;
@@ -113,6 +114,25 @@ pgwin32_putenv(const char *envval)
}
else
{
+ /*+ * Pin this DLL handle as soon as possible to avoid it+ * to be unloaded until the process terminates.+ */+ if (!rtmodules[i].pinned)+ {+ /*+ * This leaks the new reference, but that does+ * not matter because we pin the DLL anyway.+ */+ HMODULE tmp;+ BOOL res = GetModuleHandleEx(+ GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS+ | GET_MODULE_HANDLE_EX_FLAG_PIN,+ (LPCTSTR)rtmodules[i].hmodule,+ &tmp);+ rtmodules[i].pinned = res != 0;+ }+
rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv");
if (rtmodules[i].putenvFunc == NULL)
{
--
2.10.2.windows.1
0001-whitespace.patch (application/octet-stream, 2.1K) download
From fefe3a4f571b966e5cd84967c135acbe82fe8d37 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 16:07:01 +0100
Subject: [PATCH] Cosmetic fixes (whitespace, NULL instead of 0).
---
src/port/win32env.c | 42 +++++++++++-------------------------------
1 file changed, 11 insertions(+), 31 deletions(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex e64065c..478ec1d 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -45,36 +45,16 @@ pgwin32_putenv(const char *envval)
PUTENVPROC putenvFunc;
} rtmodules[] =
{
- {- "msvcrt", 0, NULL- }, /* Visual Studio 6.0 / mingw */- {- "msvcr70", 0, NULL- }, /* Visual Studio 2002 */- {- "msvcr71", 0, NULL- }, /* Visual Studio 2003 */- {- "msvcr80", 0, NULL- }, /* Visual Studio 2005 */- {- "msvcr90", 0, NULL- }, /* Visual Studio 2008 */- {- "msvcr100", 0, NULL- }, /* Visual Studio 2010 */- {- "msvcr110", 0, NULL- }, /* Visual Studio 2012 */- {- "msvcr120", 0, NULL- }, /* Visual Studio 2013 */- {- "ucrtbase", 0, NULL- }, /* Visual Studio 2015 and later */- {- NULL, 0, NULL- }+ { "msvcrt", NULL, NULL }, /* Visual Studio 6.0 / mingw */+ { "msvcr70", NULL, NULL }, /* Visual Studio 2002 */+ { "msvcr71", NULL, NULL }, /* Visual Studio 2003 */+ { "msvcr80", NULL, NULL }, /* Visual Studio 2005 */+ { "msvcr90", NULL, NULL }, /* Visual Studio 2008 */+ { "msvcr100", NULL, NULL }, /* Visual Studio 2010 */+ { "msvcr110", NULL, NULL }, /* Visual Studio 2012 */+ { "msvcr120", NULL, NULL }, /* Visual Studio 2013 */+ { "ucrtbase", NULL, NULL }, /* Visual Studio 2015 and later */+ { NULL, NULL, NULL }
};
int i;
@@ -82,7 +62,7 @@ pgwin32_putenv(const char *envval)
{
if (rtmodules[i].putenvFunc == NULL)
{
- if (rtmodules[i].hmodule == 0)+ if (rtmodules[i].hmodule == NULL)
{
/* Not attempted before, so try to find this DLL */
rtmodules[i].hmodule = GetModuleHandle(rtmodules[i].modulename);
--
2.10.2.windows.1
0002-closehandle.patch (application/octet-stream, 809B) download
From fb9bb4c57c99a2446397b04622164804bd29d4cc Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 16:11:27 +0100
Subject: [PATCH] Remove unnecessary CloseHandle() call.
An HMODULE from GetModuleHandle() does not need closing.
---
src/port/win32env.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex 478ec1d..5aaf100 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -80,7 +80,6 @@ pgwin32_putenv(const char *envval)
rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv");
if (rtmodules[i].putenvFunc == NULL)
{
- CloseHandle(rtmodules[i].hmodule);
rtmodules[i].hmodule = INVALID_HANDLE_VALUE;
continue;
}
--
2.10.2.windows.1
0003-debug-crt.patch (application/octet-stream, 1.9K) download
From 6e4dff144c6f3d82941ef6a7b31cbea6ca82180e Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 16:14:04 +0100
Subject: [PATCH] Add support for updating the environment in debug CRTs.
---
src/port/win32env.c | 27 ++++++++++++++++++---------
1 file changed, 18 insertions(+), 9 deletions(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex 5aaf100..2047b03 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -45,15 +45,24 @@ pgwin32_putenv(const char *envval)
PUTENVPROC putenvFunc;
} rtmodules[] =
{
- { "msvcrt", NULL, NULL }, /* Visual Studio 6.0 / mingw */- { "msvcr70", NULL, NULL }, /* Visual Studio 2002 */- { "msvcr71", NULL, NULL }, /* Visual Studio 2003 */- { "msvcr80", NULL, NULL }, /* Visual Studio 2005 */- { "msvcr90", NULL, NULL }, /* Visual Studio 2008 */- { "msvcr100", NULL, NULL }, /* Visual Studio 2010 */- { "msvcr110", NULL, NULL }, /* Visual Studio 2012 */- { "msvcr120", NULL, NULL }, /* Visual Studio 2013 */- { "ucrtbase", NULL, NULL }, /* Visual Studio 2015 and later */+ { "msvcrt", NULL, NULL },+ { "msvcrtd", NULL, NULL }, /* Visual Studio 6.0 / mingw */+ { "msvcr70", NULL, NULL },+ { "msvcr70d", NULL, NULL }, /* Visual Studio 2002 */+ { "msvcr71", NULL, NULL },+ { "msvcr71d", NULL, NULL }, /* Visual Studio 2003 */+ { "msvcr80", NULL, NULL },+ { "msvcr80d", NULL, NULL }, /* Visual Studio 2005 */+ { "msvcr90", NULL, NULL },+ { "msvcr90d", NULL, NULL }, /* Visual Studio 2008 */+ { "msvcr100", NULL, NULL },+ { "msvcr100d", NULL, NULL }, /* Visual Studio 2010 */+ { "msvcr110", NULL, NULL },+ { "msvcr110d", NULL, NULL }, /* Visual Studio 2012 */+ { "msvcr120", NULL, NULL },+ { "msvcr120d", NULL, NULL }, /* Visual Studio 2013 */+ { "ucrtbase", NULL, NULL },+ { "ucrtbased", NULL, NULL }, /* Visual Studio 2015 and later */
{ NULL, NULL, NULL }
};
int i;
--
2.10.2.windows.1
0004 no-caching-notfound.patch (application/octet-stream, 1.6K) download
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.cindex 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
0005-reorder-update.patch (application/octet-stream, 2.2K) download
From ffb37978ba120522eccf2c0849e46edd94afde67 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 16:47:21 +0100
Subject: [PATCH] Do the process environment update first.
This closes a potential race that can happen if a new CRT is loaded while
pgwin32_putenv() is running. Per Noah Misch.
---
src/port/win32env.c | 64 ++++++++++++++++++++++++++---------------------------
1 file changed, 32 insertions(+), 32 deletions(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex 7bccee6..d6391b0 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -24,6 +24,38 @@ pgwin32_putenv(const char *envval)
char *cp;
/*
+ * Update the process environment - to make modifications visible to child+ * processes.+ *+ * Need a copy of the string so we can modify it.+ */+ envcpy = strdup(envval);+ if (!envcpy)+ return -1;+ cp = strchr(envcpy, '=');+ if (cp == NULL)+ {+ free(envcpy);+ return -1;+ }+ *cp = '\0';+ cp++;+ if (strlen(cp))+ {+ /*+ * Only call SetEnvironmentVariable() when we are adding a variable,+ * not when removing it. Calling it on both crashes on at least+ * certain versions of MingW.+ */+ if (!SetEnvironmentVariable(envcpy, cp))+ {+ free(envcpy);+ return -1;+ }+ }+ free(envcpy);++ /*
* Each version of MSVCRT has its own _putenv() call in the runtime
* library.
*
@@ -102,38 +134,6 @@ pgwin32_putenv(const char *envval)
}
#endif /* _MSC_VER */
- /*- * Update the process environment - to make modifications visible to child- * processes.- *- * Need a copy of the string so we can modify it.- */- envcpy = strdup(envval);- if (!envcpy)- return -1;- cp = strchr(envcpy, '=');- if (cp == NULL)- {- free(envcpy);- return -1;- }- *cp = '\0';- cp++;- if (strlen(cp))- {- /*- * Only call SetEnvironmentVariable() when we are adding a variable,- * not when removing it. Calling it on both crashes on at least- * certain versions of MingW.- */- if (!SetEnvironmentVariable(envcpy, cp))- {- free(envcpy);- return -1;- }- }- free(envcpy);-
/* Finally, update our "own" cache */
return _putenv(envval);
}
--
2.10.2.windows.1
0006-no-caching-at-all.patch (application/octet-stream, 3.9K) download
From 84d69b98f9ae5cfe704cef3af44ea42d0a549d73 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 16:55:45 +0100
Subject: [PATCH] Fix the unload race as best we can.
The fix is, unfortunately, to get rid of all caching.
After fixing the load race in the previous commit, a situation was possible *)
where a CRT is loaded, pgwin32_putenv() is called, and then later that CRT is
unloaded again. The next call to pgwin32_putenv() would then attempt to use
the cached function address and crash.
*) It had been possible for untold years, but the probability was very low
because the first call to pgwin32_putenv() occurs early in the process
and used to finalize the list of CRTs the function will admit exist --
whatever is loaded at that point is likely to stay loaded.
---
src/port/win32env.c | 84 ++++++++++++++++++++---------------------------------
1 file changed, 32 insertions(+), 52 deletions(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex d6391b0..25eed06 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -70,67 +70,47 @@ pgwin32_putenv(const char *envval)
*/
#ifdef _MSC_VER
typedef int (_cdecl * PUTENVPROC) (const char *);
- static struct+ static const char *modulenames[] =
{
- char *modulename;- HMODULE hmodule;- PUTENVPROC putenvFunc;- } rtmodules[] =- {- { "msvcrt", NULL, NULL },- { "msvcrtd", NULL, NULL }, /* Visual Studio 6.0 / mingw */- { "msvcr70", NULL, NULL },- { "msvcr70d", NULL, NULL }, /* Visual Studio 2002 */- { "msvcr71", NULL, NULL },- { "msvcr71d", NULL, NULL }, /* Visual Studio 2003 */- { "msvcr80", NULL, NULL },- { "msvcr80d", NULL, NULL }, /* Visual Studio 2005 */- { "msvcr90", NULL, NULL },- { "msvcr90d", NULL, NULL }, /* Visual Studio 2008 */- { "msvcr100", NULL, NULL },- { "msvcr100d", NULL, NULL }, /* Visual Studio 2010 */- { "msvcr110", NULL, NULL },- { "msvcr110d", NULL, NULL }, /* Visual Studio 2012 */- { "msvcr120", NULL, NULL },- { "msvcr120d", NULL, NULL }, /* Visual Studio 2013 */- { "ucrtbase", NULL, NULL },- { "ucrtbased", NULL, NULL }, /* Visual Studio 2015 and later */- { NULL, NULL, NULL }+ "msvcrt",+ "msvcrtd", /* Visual Studio 6.0 / mingw */+ "msvcr70",+ "msvcr70d", /* Visual Studio 2002 */+ "msvcr71",+ "msvcr71d", /* Visual Studio 2003 */+ "msvcr80",+ "msvcr80d", /* Visual Studio 2005 */+ "msvcr90",+ "msvcr90d", /* Visual Studio 2008 */+ "msvcr100",+ "msvcr100d", /* Visual Studio 2010 */+ "msvcr110",+ "msvcr110d", /* Visual Studio 2012 */+ "msvcr120",+ "msvcr120d", /* Visual Studio 2013 */+ "ucrtbase",+ "ucrtbased", /* Visual Studio 2015 and later */+ NULL
};
int i;
- for (i = 0; rtmodules[i].modulename; i++)+ /*+ * Call the _putenv() function in all loaded CRTs.+ * We cannot cache any information about loaded DLLs or+ * export addresses in them because this can change at+ * runtime.+ */+ for (i = 0; modulenames[i]; i++)
{
- if (rtmodules[i].putenvFunc == NULL)+ HMODULE hmodule = GetModuleHandle(modulenames[i]);+ if (hmodule)
{
- if (rtmodules[i].hmodule == NULL)- {- /* Try to find this DLL */- rtmodules[i].hmodule = GetModuleHandle(rtmodules[i].modulename);- if (rtmodules[i].hmodule == NULL)- {- continue;- }- else- {- rtmodules[i].putenvFunc = (PUTENVPROC) GetProcAddress(rtmodules[i].hmodule, "_putenv");- if (rtmodules[i].putenvFunc == NULL)- {- continue;- }- }- }- else+ PUTENVPROC putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv");+ if (putenvFunc)
{
- /*- * Module loaded, but we did not find the function last time.- * We're not going to find it this time either...- */- continue;+ putenvFunc(envval);
}
}
- /* At this point, putenvFunc is set or we have exited the loop */- rtmodules[i].putenvFunc(envval);
}
#endif /* _MSC_VER */
--
2.10.2.windows.1
0007-getmodulehandleex-correctness.patch (application/octet-stream, 1.3K) download
From 6aafcece307197d7f453b081093b35c5ba00c821 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <chris@chrullrich.net>
Date: Wed, 16 Nov 2016 17:02:27 +0100
Subject: [PATCH] Avoid a possible crash.
Although PostgreSQL itself does not use multithreading, there are always a
number of threads in a process on Windows. If one of them causes a CRT to
unload at the wrong time, we can crash in the _putenv() call. Fix by
keeping our own reference to the loaded DLL as long as we need.
Per suggestion from Noah Misch.
---
src/port/win32env.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/port/win32env.c b/src/port/win32env.cindex 25eed06..5b594e3 100644--- a/src/port/win32env.c+++ b/src/port/win32env.c@@ -102,14 +102,20 @@ pgwin32_putenv(const char *envval)
*/
for (i = 0; modulenames[i]; i++)
{
- HMODULE hmodule = GetModuleHandle(modulenames[i]);- if (hmodule)+ /*+ * Make sure nothing else unloads the DLL while we are using it+ * by creating our own reference to it.+ */+ HMODULE hmodule = NULL;+ BOOL res = GetModuleHandleEx(0, modulenames[i], &hmodule);+ if (res != 0 && hmodule != NULL)
{
PUTENVPROC putenvFunc = (PUTENVPROC) GetProcAddress(hmodule, "_putenv");
if (putenvFunc)
{
putenvFunc(envval);
}
+ FreeLibrary(hmodule);
}
}
#endif /* _MSC_VER */
--
2.10.2.windows.1
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: chris@chrullrich.net, noah@leadboat.com, andrew@dunslane.net, magnus@hagander.net, pgsql-hackers@postgresql.org, michael.paquier@gmail.com
Subject: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013
In-Reply-To: <DB6PR0602MB2981431251C29DBAB293A6CAD4BE0@DB6PR0602MB2981.eurprd06.prod.outlook.com>
References: <E1atXIK-0003un-G0@gemulon.postgresql.org> <b0c66170-5711-20dd-2de3-408429ca0f48@chrullrich.net> <CABUevEzicpYAUOF9ZHkee1-c-gvokKAmr708sqta4H71bOZOWQ@mail.gmail.com> <34dd7956-3324-0305-288d-edc7d56fba76@chrullrich.net> <571E6D90.7050903@dunslane.net> <9d5c6a49-774c-44de-83fc-65c5b431f8d9@chrullrich.net> <2c5f9472-d0cc-df61-78e3-bc48909ffb59@chrullrich.net> <784524b2-bd50-1f5e-dad9-f8840f97753a@chrullrich.net> <20161116072246.GA2297058@tornado.leadboat.com> <DB6PR0602MB2981431251C29DBAB293A6CAD4BE0@DB6PR0602MB2981.eurprd06.prod.outlook.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox