winansi: avoid buffer overrun

When we could not convert the UTF-8 sequence into Unicode for writing to the Console, we should not try to write an insanely-long sequence of invalid wide characters (mistaking the negative return value for an unsigned length). Reported by Coverity. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed May 4, 2017 at 15:55 UTC b6b066adf9e1e970a6d8295db630ab1e1f3bc71c
1 file changed +5
compat/winansi.c
+5
@@ -140,6 +140,11 @@ static void write_console(unsigned char *str, size_t len)
140
141 /* convert utf-8 to utf-16 */
142 int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
143 + if (wlen < 0) {
144 + wchar_t *err = L"[invalid]";
145 + WriteConsoleW(console, err, wcslen(err), &dummy, NULL);
146 + return;
147 + }
148
149 /* write directly to console */
150 WriteConsoleW(console, wbuf, wlen, &dummy, NULL);