]> git.ipfire.org Git - thirdparty/git.git/commitdiff
winansi: avoid buffer overrun
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Thu, 4 May 2017 13:55:34 +0000 (15:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 May 2017 03:18:19 +0000 (12:18 +0900)
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>
compat/winansi.c

index a551de90eb042804690ab4dbef6beee948693285..a11a0f16d276470381587236ae513994d92af477 100644 (file)
@@ -140,6 +140,11 @@ static void write_console(unsigned char *str, size_t len)
 
        /* convert utf-8 to utf-16 */
        int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
+       if (wlen < 0) {
+               wchar_t *err = L"[invalid]";
+               WriteConsoleW(console, err, wcslen(err), &dummy, NULL);
+               return;
+       }
 
        /* write directly to console */
        WriteConsoleW(console, wbuf, wlen, &dummy, NULL);