Skip to content

Commit 4043d4b

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 68fc36a commit 4043d4b

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
@@ -1012,6 +1012,10 @@ static int do_putenv(char **env, const char *name, int size, int free_old);
10121012
static int environ_size = 0;
10131013
/* allocated size of environ array, in bytes */
10141014
static int environ_alloc = 0;
1015+
/* used as a indicator when the environment has been changed outside mingw.c */
1016+
static char **saved_environ;
1017+
1018+
static void maybe_reinitialize_environ(void);
10151019

10161020
/*
10171021
* Create environment block suitable for CreateProcess. Merges current
@@ -1021,7 +1025,10 @@ static wchar_t *make_environment_block(char **deltaenv)
10211025
{
10221026
wchar_t *wenvblk = NULL;
10231027
char **tmpenv;
1024-
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
1028+
int i = 0, size, wenvsz = 0, wenvpos = 0;
1029+
1030+
maybe_reinitialize_environ();
1031+
size = environ_size;
10251032

10261033
while (deltaenv && deltaenv[i] && *deltaenv[i])
10271034
i++;
@@ -1324,6 +1331,41 @@ static int compareenv(const void *v1, const void *v2)
13241331
}
13251332
}
13261333

1334+
/*
1335+
* Functions implemented outside Git are able to modify the environment,
1336+
* too. For example, cURL's curl_global_init() function sets the CHARSET
1337+
* environment variable (at least in certain circumstances).
1338+
*
1339+
* Therefore we need to be *really* careful *not* to assume that we have
1340+
* sole control over the environment and reinitalize it when necessary.
1341+
*/
1342+
static void maybe_reinitialize_environ(void)
1343+
{
1344+
int i;
1345+
1346+
if (!saved_environ) {
1347+
warning("MinGW environment not initialized yet");
1348+
return;
1349+
}
1350+
1351+
if (environ_size <= 0)
1352+
return;
1353+
1354+
if (saved_environ != environ)
1355+
/* We have *no* idea how much space was allocated outside */
1356+
environ_alloc = 0;
1357+
else if (!environ[environ_size - 1])
1358+
return; /* still consistent */
1359+
1360+
for (i = 0; environ[i] && *environ[i]; i++)
1361+
; /* continue counting */
1362+
environ[i] = NULL;
1363+
environ_size = i + 1;
1364+
1365+
/* sort environment for O(log n) getenv / putenv */
1366+
qsort(environ, i, sizeof(char*), compareenv);
1367+
}
1368+
13271369
static int bsearchenv(char **env, const char *name, size_t size)
13281370
{
13291371
unsigned low = 0, high = size;
@@ -1377,6 +1419,7 @@ char *mingw_getenv(const char *name)
13771419
if (environ_size <= 0)
13781420
return NULL;
13791421

1422+
maybe_reinitialize_environ();
13801423
pos = bsearchenv(environ, name, environ_size - 1);
13811424

13821425
if (pos < 0)
@@ -1387,7 +1430,9 @@ char *mingw_getenv(const char *name)
13871430

13881431
int mingw_putenv(const char *namevalue)
13891432
{
1433+
maybe_reinitialize_environ();
13901434
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
1435+
saved_environ = environ;
13911436
environ_size = do_putenv(environ, namevalue, environ_size, 1);
13921437
return 0;
13931438
}
@@ -2308,7 +2353,7 @@ void mingw_startup()
23082353
*/
23092354
environ_size = i + 1;
23102355
environ_alloc = alloc_nr(environ_size * sizeof(char*));
2311-
environ = malloc_startup(environ_alloc);
2356+
saved_environ = environ = malloc_startup(environ_alloc);
23122357

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

0 commit comments

Comments
 (0)