Skip to content

Commit 7c98dee

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 12aa6cf commit 7c98dee

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
@@ -968,19 +968,19 @@ static wchar_t *make_environment_block(char **deltaenv)
968968
char **tmpenv;
969969
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
970970

971-
while (deltaenv && deltaenv[i])
971+
while (deltaenv && deltaenv[i] && *deltaenv[i])
972972
i++;
973973

974974
/* copy the environment, leaving space for changes */
975975
tmpenv = xmalloc((size + i) * sizeof(char*));
976976
memcpy(tmpenv, environ, size * sizeof(char*));
977977

978978
/* merge supplied environment changes into the temporary environment */
979-
for (i = 0; deltaenv && deltaenv[i]; i++)
979+
for (i = 0; deltaenv && deltaenv[i] && *deltaenv[i]; i++)
980980
size = do_putenv(tmpenv, deltaenv[i], size, 0);
981981

982982
/* create environment block from temporary environment */
983-
for (i = 0; tmpenv[i]; i++) {
983+
for (i = 0; tmpenv[i] && *tmpenv[i]; i++) {
984984
size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */
985985
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
986986
wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1;

0 commit comments

Comments
 (0)