Skip to content

Commit 6432af8

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 66d2714 commit 6432af8

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
@@ -916,19 +916,19 @@ static wchar_t *make_environment_block(char **deltaenv)
916916
char **tmpenv;
917917
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
918918

919-
while (deltaenv && deltaenv[i])
919+
while (deltaenv && deltaenv[i] && *deltaenv[i])
920920
i++;
921921

922922
/* copy the environment, leaving space for changes */
923923
tmpenv = xmalloc((size + i) * sizeof(char*));
924924
memcpy(tmpenv, environ, size * sizeof(char*));
925925

926926
/* merge supplied environment changes into the temporary environment */
927-
for (i = 0; deltaenv && deltaenv[i]; i++)
927+
for (i = 0; deltaenv && deltaenv[i] && *deltaenv[i]; i++)
928928
size = do_putenv(tmpenv, deltaenv[i], size, 0);
929929

930930
/* create environment block from temporary environment */
931-
for (i = 0; tmpenv[i]; i++) {
931+
for (i = 0; tmpenv[i] && *tmpenv[i]; i++) {
932932
size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */
933933
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
934934
wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1;

0 commit comments

Comments
 (0)