From edfe9453be5f8ba374da0ab860be863d66965c56 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 22 Jun 2021 11:08:05 +0200 Subject: [PATCH] w32: Add fallback in case the Windows console can't cope with Unicode. * common/ttyio.c (w32_write_console): Fallback to WriteConsoleA on error. -- To test this switch the Windows Console to "legacy mode" set LANG=de gpg --card-edit and enter an invalid command. The response contains an Umlaut and old Windows versions (and the legacy console) don't have a proper font installed for this. Without this patch this runs into a log_fatal error. The mitigation we implement is to fallback to WriteConsoleA, that is accepting wrong encoding and to print a note about the problem. GnuPG-bug-id: 5491 --- common/ttyio.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/common/ttyio.c b/common/ttyio.c index 15f7a0203..f77c053e2 100644 --- a/common/ttyio.c +++ b/common/ttyio.c @@ -236,10 +236,24 @@ w32_write_console (const char *string) n = wcslen (wstring); if (!WriteConsoleW (con.out, wstring, n, &nwritten, NULL)) - log_fatal ("WriteConsole failed: %s", w32_strerror (-1)); - if (n != nwritten) - log_fatal ("WriteConsole failed: %lu != %lu\n", - (unsigned long)n, (unsigned long)nwritten); + { + static int shown; + if (!shown) + { + shown = 1; + log_info ("WriteConsole failed: %s", w32_strerror (-1)); + log_info ("Please configure a suitable font for the console\n"); + } + n = strlen (string); + if (!WriteConsoleA (con.out, string, n , &nwritten, NULL)) + log_fatal ("WriteConsole fallback failed: %s", w32_strerror (-1)); + } + else + { + if (n != nwritten) + log_fatal ("WriteConsole failed: %lu != %lu\n", + (unsigned long)n, (unsigned long)nwritten); + } last_prompt_len += n; xfree (wstring); }