Skip to content

Commit 22cc9c9

Browse files
committed
mingw: be *very* wary about outside environment changes
The environment is modified in most surprising circumstances, and not all of them are under Git's control. For example, calling curl_global_init() on Windows will ensure that the CHARSET variable is set, adding one if necessary. While the previous commit worked around crashes triggered by such outside changes of the environment by relaxing the requirement that the environment be terminated by a NULL pointer, the other assumption made by `mingw_getenv()` and `mingw_putenv()` is that the environment is sorted, for efficient lookup via binary search. Let's make real sure that our environment is intact before querying or modifying it, and reinitialize our idea of the environment if necessary. With this commit, before working on the environment we look briefly for indicators that the environment was modified outside of our control, and to ensure that it is terminated with a NULL pointer and sorted again in that case. Note: the indicators are maybe not sufficient. For example, when a variable is removed, it will not be noticed. It might also be a problem if outside changes to the environment result in a modified `environ` pointer: it is unclear whether such a modification could result in a problem when `mingw_putenv()` needs to `realloc()` the environment buffer. For the moment, however, the current fix works well enough, so let's only face the potential problems when (and if!) they occur. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ab16d5e commit 22cc9c9

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

compat/mingw.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ static int do_putenv(char **env, const char *name, int size, int free_old);
966966
static int environ_size = 0;
967967
/* allocated size of environ array, in bytes */
968968
static int environ_alloc = 0;
969+
/* used as a indicator when the environment has been changed outside mingw.c */
970+
static char **saved_environ;
971+
972+
static void maybe_reinitialize_environ(void);
969973

970974
/*
971975
* Create environment block suitable for CreateProcess. Merges current
@@ -975,7 +979,10 @@ static wchar_t *make_environment_block(char **deltaenv)
975979
{
976980
wchar_t *wenvblk = NULL;
977981
char **tmpenv;
978-
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
982+
int i = 0, size, wenvsz = 0, wenvpos = 0;
983+
984+
maybe_reinitialize_environ();
985+
size = environ_size;
979986

980987
while (deltaenv && deltaenv[i] && *deltaenv[i])
981988
i++;
@@ -1278,6 +1285,41 @@ static int compareenv(const void *v1, const void *v2)
12781285
}
12791286
}
12801287

1288+
/*
1289+
* Functions implemented outside Git are able to modify the environment,
1290+
* too. For example, cURL's curl_global_init() function sets the CHARSET
1291+
* environment variable (at least in certain circumstances).
1292+
*
1293+
* Therefore we need to be *really* careful *not* to assume that we have
1294+
* sole control over the environment and reinitalize it when necessary.
1295+
*/
1296+
static void maybe_reinitialize_environ(void)
1297+
{
1298+
int i;
1299+
1300+
if (!saved_environ) {
1301+
warning("MinGW environment not initialized yet");
1302+
return;
1303+
}
1304+
1305+
if (environ_size <= 0)
1306+
return;
1307+
1308+
if (saved_environ != environ)
1309+
/* We have *no* idea how much space was allocated outside */
1310+
environ_alloc = 0;
1311+
else if (!environ[environ_size - 1])
1312+
return; /* still consistent */
1313+
1314+
for (i = 0; environ[i] && *environ[i]; i++)
1315+
; /* continue counting */
1316+
environ[i] = NULL;
1317+
environ_size = i + 1;
1318+
1319+
/* sort environment for O(log n) getenv / putenv */
1320+
qsort(environ, i, sizeof(char*), compareenv);
1321+
}
1322+
12811323
static int bsearchenv(char **env, const char *name, size_t size)
12821324
{
12831325
unsigned low = 0, high = size;
@@ -1331,6 +1373,7 @@ char *mingw_getenv(const char *name)
13311373
if (environ_size <= 0)
13321374
return NULL;
13331375

1376+
maybe_reinitialize_environ();
13341377
pos = bsearchenv(environ, name, environ_size - 1);
13351378

13361379
if (pos < 0)
@@ -1341,7 +1384,9 @@ char *mingw_getenv(const char *name)
13411384

13421385
int mingw_putenv(const char *namevalue)
13431386
{
1387+
maybe_reinitialize_environ();
13441388
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
1389+
saved_environ = environ;
13451390
environ_size = do_putenv(environ, namevalue, environ_size, 1);
13461391
return 0;
13471392
}
@@ -2229,7 +2274,7 @@ void mingw_startup()
22292274
*/
22302275
environ_size = i + 1;
22312276
environ_alloc = alloc_nr(environ_size * sizeof(char*));
2232-
environ = malloc_startup(environ_alloc);
2277+
saved_environ = environ = malloc_startup(environ_alloc);
22332278

22342279
/* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
22352280
maxlen = 3 * maxlen + 1;

0 commit comments

Comments
 (0)