Skip to content

Commit 7ce9b0d

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
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 <vano@mail.mipt.ru> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 2c666fa commit 7ce9b0d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

compat/mingw.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2844,6 +2844,18 @@ static size_t append_system_bin_dirs(char *path, size_t size)
28442844
#endif
28452845
#endif
28462846

2847+
static int is_system32_path(const char *path)
2848+
{
2849+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2850+
2851+
if (xutftowcs_path(wpath, path) < 0 ||
2852+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
2853+
_wcsicmp(system32, wpath))
2854+
return 0;
2855+
2856+
return 1;
2857+
}
2858+
28472859
static void setup_windows_environment(void)
28482860
{
28492861
char *tmp = getenv("TMPDIR");
@@ -2884,7 +2896,8 @@ static void setup_windows_environment(void)
28842896
strbuf_addstr(&buf, tmp);
28852897
if ((tmp = getenv("HOMEPATH"))) {
28862898
strbuf_addstr(&buf, tmp);
2887-
if (is_directory(buf.buf))
2899+
if (!is_system32_path(buf.buf) &&
2900+
is_directory(buf.buf))
28882901
setenv("HOME", buf.buf, 1);
28892902
else
28902903
tmp = NULL; /* use $USERPROFILE */

0 commit comments

Comments
 (0)