Skip to content

Commit 3a532ba

Browse files
committed
mingw: be more defensive when making the environment block
Outside of our Windows-specific code, the end of the environment can be marked also by a pointer to a NUL character, not only by a NULL pointer as our code assumed so far. That led to a buffer overrun in `make_environment_block()` when running `git-remote-https` in `mintty` (because `curl_global_init()` added the `CHARSET` environment variable *outside* of `mingw_putenv()`, ending the environment in a pointer to an empty string). Side note for future debugging on Windows: when running programs in `mintty`, the standard input/output/error is not connected to a Win32 Console, but instead is pipe()d. That means that even stderr may not be written completely before a crash, but has to be fflush()ed explicitly. For example, when debugging crashes, the developer should insert an `fflush(stderr);` at the end of the `error()` function defined in usage.c. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 18a389c commit 3a532ba

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

compat/mingw.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,19 +1057,19 @@ static wchar_t *make_environment_block(char **deltaenv)
10571057
char **tmpenv;
10581058
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
10591059

1060-
while (deltaenv && deltaenv[i])
1060+
while (deltaenv && deltaenv[i] && *deltaenv[i])
10611061
i++;
10621062

10631063
/* copy the environment, leaving space for changes */
10641064
ALLOC_ARRAY(tmpenv, size + i);
10651065
memcpy(tmpenv, environ, size * sizeof(char*));
10661066

10671067
/* merge supplied environment changes into the temporary environment */
1068-
for (i = 0; deltaenv && deltaenv[i]; i++)
1068+
for (i = 0; deltaenv && deltaenv[i] && *deltaenv[i]; i++)
10691069
size = do_putenv(tmpenv, deltaenv[i], size, 0);
10701070

10711071
/* create environment block from temporary environment */
1072-
for (i = 0; tmpenv[i]; i++) {
1072+
for (i = 0; tmpenv[i] && *tmpenv[i]; i++) {
10731073
size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */
10741074
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
10751075
wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1;

0 commit comments

Comments
 (0)