Skip to content

Commit 064718e

Browse files
committed
mingw: ignore HOMEDRIVE/HOMEPATH if it points to Windows' system directory
Internally, Git expects the environment variable `HOME` to be set, and to point to the current user's home directory. This environment variable is not set by default on Windows, and therefore Git tries its best to construct one if it finds `HOME` unset. There are actually two different approaches Git tries: first, it looks at `HOMEDRIVE`/`HOMEPATH` because this is widely used in corporate environments with roaming profiles, and a user generally wants their global Git settings to be in a roaming profile. Only when `HOMEDRIVE`/`HOMEPATH` is either unset or does not point to a valid location, Git will fall back to using `USERPROFILE` instead. However, starting with Windows Vista, for secondary logons and services, the environment variables `HOMEDRIVE`/`HOMEPATH` point to Windows' system directory (usually `C:\Windows\system32`). That is undesirable, and that location is usually write-protected anyway. So let's verify that the `HOMEDRIVE`/`HOMEPATH` combo does not point to Windows' system directory before using it, falling back to `USERPROFILE` if it does. This fixes git-for-windows#2709 Initial-Path-by: Ivan Pozdeev <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 5300e52 commit 064718e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

compat/mingw.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,18 @@ static size_t append_system_bin_dirs(char *path, size_t size)
32893289
}
32903290
#endif
32913291

3292+
static int is_system32_path(const char *path)
3293+
{
3294+
WCHAR system32[MAX_LONG_PATH], wpath[MAX_LONG_PATH];
3295+
3296+
if (xutftowcs_long_path(wpath, path) < 0 ||
3297+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
3298+
_wcsicmp(system32, wpath))
3299+
return 0;
3300+
3301+
return 1;
3302+
}
3303+
32923304
static void setup_windows_environment(void)
32933305
{
32943306
char *tmp = getenv("TMPDIR");
@@ -3329,7 +3341,8 @@ static void setup_windows_environment(void)
33293341
strbuf_addstr(&buf, tmp);
33303342
if ((tmp = getenv("HOMEPATH"))) {
33313343
strbuf_addstr(&buf, tmp);
3332-
if (is_directory(buf.buf))
3344+
if (!is_system32_path(buf.buf) &&
3345+
is_directory(buf.buf))
33333346
setenv("HOME", buf.buf, 1);
33343347
else
33353348
tmp = NULL; /* use $USERPROFILE */

0 commit comments

Comments
 (0)