* Michael Paquier wrote:

On Thu, Sep 1, 2016 at 4:03 PM, Christian Ullrich <ch...@chrullrich.net> wrote:


My conclusion from April stands:

The fact that master looks like it does means that there have not been
many (or any) complaints about missing cross-module environment
variables. If nobody ever needs to see a variable set elsewhere, we
have a very simple solution: Why don't we simply throw out the whole
#ifdef _MSC_VER block?

Looking at the commit logs and 741e4ad7 that does not sound like a good idea.

Well, I still maintain that if it doesn't work and has never worked, getting rid of it is better than making it work six years after the fact. OTOH, there may have been cases where it did actually work, perhaps those gnuwin32 libraries that were mentioned in the comment before it was changed in that commit above, if they were loaded before the first call to the function.

OTTH, wouldn't it be funny if fixing it actually broke something that worked accidentally because it *didn't* get the updated environment? I think that is at least as likely as suddenly getting excited reports that something now works that hasn't before.

It is better to avoid !!. See for example 1a7a436 that avoided
problems with VS2015 as far as I recall.

Agreed, thanks for noticing. This change creates a warning, however, because GetModuleHandleEx() returns BOOL, not HMODULE. Updated 0003 attached, simplified over my original one.

In order to avoid any problems with the load and unload windows, my
bet goes for 0001, 0002 and 0003, with the last two patches merged
together, 0001 being only a set of independent fixes. That's ugly, but
that looks the safest course of actions. I have rebased/rewritten the
patches as attached. Thoughts?

In lieu of convincing you to drop the entire thing, yes, that looks about right, except for the BOOL thing.

--
Christian

>From dfbe7384b309c20d7733b0d18e6819302a483d95 Mon Sep 17 00:00:00 2001
From: Christian Ullrich <ch...@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 | 54 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 19 deletions(-)

diff --git a/src/port/win32env.c b/src/port/win32env.c
index 3f56ba8..7417b60 100644
--- a/src/port/win32env.c
+++ b/src/port/win32env.c
@@ -43,36 +43,37 @@ pgwin32_putenv(const char *envval)
                char       *modulename;
                HMODULE         hmodule;
                PUTENVPROC      putenvFunc;
+               BOOL            pinned;
        }                       rtmodules[] =
        {
                /* Visual Studio 6.0 / mingw */
-               {"msvcrt",              NULL,   NULL},
-               {"msvcrtd",             NULL,   NULL},
+               {"msvcrt",              NULL,   NULL,   FALSE},
+               {"msvcrtd",             NULL,   NULL,   FALSE},
                /* Visual Studio 2002 */
-               {"msvcr70",             NULL,   NULL},
-               {"msvcr70d",    NULL,   NULL},
+               {"msvcr70",             NULL,   NULL,   FALSE},
+               {"msvcr70d",    NULL,   NULL,   FALSE},
                /* Visual Studio 2003 */
-               {"msvcr71",             NULL,   NULL},
-               {"msvcr71d",    NULL,   NULL},
+               {"msvcr71",             NULL,   NULL,   FALSE},
+               {"msvcr71d",    NULL,   NULL,   FALSE},
                /* Visual Studio 2005 */
-               {"msvcr80",             NULL,   NULL},
-               {"msvcr80d",    NULL,   NULL},
+               {"msvcr80",             NULL,   NULL,   FALSE},
+               {"msvcr80d",    NULL,   NULL,   FALSE},
                /* Visual Studio 2008 */
-               {"msvcr90",             NULL,   NULL},
-               {"msvcr90d",    NULL,   NULL},
+               {"msvcr90",             NULL,   NULL,   FALSE},
+               {"msvcr90d",    NULL,   NULL,   FALSE},
                /* Visual Studio 2010 */
-               {"msvcr100",    NULL,   NULL},
-               {"msvcr100d",   NULL,   NULL},
+               {"msvcr100",    NULL,   NULL,   FALSE},
+               {"msvcr100d",   NULL,   NULL,   FALSE},
                /* Visual Studio 2012 */
-               {"msvcr110",    NULL,   NULL},
-               {"msvcr110d",   NULL,   NULL},
+               {"msvcr110",    NULL,   NULL,   FALSE},
+               {"msvcr110d",   NULL,   NULL,   FALSE},
                /* Visual Studio 2013 */
-               {"msvcr120",    NULL,   NULL},
-               {"msvcr120d",   NULL,   NULL},
+               {"msvcr120",    NULL,   NULL,   FALSE},
+               {"msvcr120d",   NULL,   NULL,   FALSE},
                /* Visual Studio 2015 and later */
-               {"ucrtbase",    NULL,   NULL},
-               {"ucrtbased",   NULL,   NULL},
-               {NULL,                  NULL,   NULL}
+               {"ucrtbase",    NULL,   NULL,   FALSE},
+               {"ucrtbased",   NULL,   NULL,   FALSE},
+               {NULL,                  NULL,   NULL,   FALSE}
        };
        int                     i;
 
@@ -90,6 +91,21 @@ 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)
+                                       {
+                                               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;
+                                       }
+
                                        rtmodules[i].putenvFunc = (PUTENVPROC) 
GetProcAddress(rtmodules[i].hmodule, "_putenv");
                                        if (rtmodules[i].putenvFunc == NULL)
                                        {
-- 
2.10.0.windows.1

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to