Skip to content

Commit 12e5c37

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 6432af8 commit 12e5c37

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
@@ -905,6 +905,10 @@ static int do_putenv(char **env, const char *name, int size, int free_old);
905905
static int environ_size = 0;
906906
/* allocated size of environ array, in bytes */
907907
static int environ_alloc = 0;
908+
/* used as a indicator when the environment has been changed outside mingw.c */
909+
static char **saved_environ;
910+
911+
static void maybe_reinitialize_environ(void);
908912

909913
/*
910914
* Create environment block suitable for CreateProcess. Merges current
@@ -914,7 +918,10 @@ static wchar_t *make_environment_block(char **deltaenv)
914918
{
915919
wchar_t *wenvblk = NULL;
916920
char **tmpenv;
917-
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
921+
int i = 0, size, wenvsz = 0, wenvpos = 0;
922+
923+
maybe_reinitialize_environ();
924+
size = environ_size;
918925

919926
while (deltaenv && deltaenv[i] && *deltaenv[i])
920927
i++;
@@ -1216,6 +1223,41 @@ static int compareenv(const void *v1, const void *v2)
12161223
}
12171224
}
12181225

1226+
/*
1227+
* Functions implemented outside Git are able to modify the environment,
1228+
* too. For example, cURL's curl_global_init() function sets the CHARSET
1229+
* environment variable (at least in certain circumstances).
1230+
*
1231+
* Therefore we need to be *really* careful *not* to assume that we have
1232+
* sole control over the environment and reinitalize it when necessary.
1233+
*/
1234+
static void maybe_reinitialize_environ(void)
1235+
{
1236+
int i;
1237+
1238+
if (!saved_environ) {
1239+
warning("MinGW environment not initialized yet");
1240+
return;
1241+
}
1242+
1243+
if (environ_size <= 0)
1244+
return;
1245+
1246+
if (saved_environ != environ)
1247+
/* We have *no* idea how much space was allocated outside */
1248+
environ_alloc = 0;
1249+
else if (!environ[environ_size - 1])
1250+
return; /* still consistent */
1251+
1252+
for (i = 0; environ[i] && *environ[i]; i++)
1253+
; /* continue counting */
1254+
environ[i] = NULL;
1255+
environ_size = i + 1;
1256+
1257+
/* sort environment for O(log n) getenv / putenv */
1258+
qsort(environ, i, sizeof(char*), compareenv);
1259+
}
1260+
12191261
static int bsearchenv(char **env, const char *name, size_t size)
12201262
{
12211263
unsigned low = 0, high = size;
@@ -1269,6 +1311,7 @@ char *mingw_getenv(const char *name)
12691311
if (environ_size <= 0)
12701312
return NULL;
12711313

1314+
maybe_reinitialize_environ();
12721315
pos = bsearchenv(environ, name, environ_size - 1);
12731316

12741317
if (pos < 0)
@@ -1279,7 +1322,9 @@ char *mingw_getenv(const char *name)
12791322

12801323
int mingw_putenv(const char *namevalue)
12811324
{
1325+
maybe_reinitialize_environ();
12821326
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
1327+
saved_environ = environ;
12831328
environ_size = do_putenv(environ, namevalue, environ_size, 1);
12841329
return 0;
12851330
}
@@ -2104,7 +2149,7 @@ void mingw_startup()
21042149
*/
21052150
environ_size = i + 1;
21062151
environ_alloc = alloc_nr(environ_size * sizeof(char*));
2107-
environ = malloc_startup(environ_alloc);
2152+
saved_environ = environ = malloc_startup(environ_alloc);
21082153

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

0 commit comments

Comments
 (0)