From: Junio C Hamano Date: Tue, 27 Dec 2016 08:11:44 +0000 (-0800) Subject: Merge branch 'mk/mingw-winansi-ttyname-termination-fix' X-Git-Tag: v2.12.0-rc0~99 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/c5139e0e3811ea3f6014187e76b243ccf287cb83?ds=inline;hp=-c Merge branch 'mk/mingw-winansi-ttyname-termination-fix' A potential but unlikely buffer overflow in Windows port has been fixed. * mk/mingw-winansi-ttyname-termination-fix: mingw: consider that UNICODE_STRING::Length counts bytes --- c5139e0e3811ea3f6014187e76b243ccf287cb83 diff --combined compat/winansi.c index cb725fb02f,6b4f736fdc..97d84a96ed --- a/compat/winansi.c +++ b/compat/winansi.c @@@ -7,9 -7,6 +7,9 @@@ #include #include +/* In this file, we actually want to use Windows' own isatty(). */ +#undef isatty + /* ANSI codes used by git: m, K @@@ -495,7 -492,7 +495,7 @@@ static inline ioinfo* _pioinfo(int fd (fd & (IOINFO_ARRAY_ELTS - 1)) * sizeof_ioinfo); } -static int init_sizeof_ioinfo() +static int init_sizeof_ioinfo(void) { int istty, wastty; /* don't init twice */ @@@ -556,7 -553,7 +556,7 @@@ static void detect_msys_tty(int fd buffer, sizeof(buffer) - 2, &result))) return; name = nameinfo->Name.Buffer; - name[nameinfo->Name.Length] = 0; + name[nameinfo->Name.Length / sizeof(*name)] = 0; /* check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX') */ if (!wcsstr(name, L"msys-") || !wcsstr(name, L"-pty")) @@@ -573,36 -570,6 +573,36 @@@ #endif +int winansi_isatty(int fd) +{ + int res = isatty(fd); + + if (res) { + /* + * Make sure that /dev/null is not fooling Git into believing + * that we are connected to a terminal, as "_isatty() returns a + * nonzero value if the descriptor is associated with a + * character device."; for more information, see + * + * https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx + */ + HANDLE handle = (HANDLE)_get_osfhandle(fd); + if (fd == STDIN_FILENO) { + DWORD dummy; + + if (!GetConsoleMode(handle, &dummy)) + res = 0; + } else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { + CONSOLE_SCREEN_BUFFER_INFO dummy; + + if (!GetConsoleScreenBufferInfo(handle, &dummy)) + res = 0; + } + } + + return res; +} + void winansi_init(void) { int con1, con2;