Skip to content

Commit ab16d5e

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 f07ccbd commit ab16d5e

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
@@ -977,19 +977,19 @@ static wchar_t *make_environment_block(char **deltaenv)
977977
char **tmpenv;
978978
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
979979

980-
while (deltaenv && deltaenv[i])
980+
while (deltaenv && deltaenv[i] && *deltaenv[i])
981981
i++;
982982

983983
/* copy the environment, leaving space for changes */
984984
tmpenv = xmalloc((size + i) * sizeof(char*));
985985
memcpy(tmpenv, environ, size * sizeof(char*));
986986

987987
/* merge supplied environment changes into the temporary environment */
988-
for (i = 0; deltaenv && deltaenv[i]; i++)
988+
for (i = 0; deltaenv && deltaenv[i] && *deltaenv[i]; i++)
989989
size = do_putenv(tmpenv, deltaenv[i], size, 0);
990990

991991
/* create environment block from temporary environment */
992-
for (i = 0; tmpenv[i]; i++) {
992+
for (i = 0; tmpenv[i] && *tmpenv[i]; i++) {
993993
size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */
994994
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
995995
wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1;

0 commit comments

Comments
 (0)