From 4890074e68e3a4065b744ac99a854f95e7442b57 Mon Sep 17 00:00:00 2001 From: dEajL3kA Date: Fri, 16 Jan 2026 11:20:48 +0100 Subject: [PATCH] winapi: use FormatMessageA instead of FormatMessageW Use FormatMessageA() to get the error message as multibyte-character string (local codepage) directly, instead of using FormatMessageW() and then convert the string from Unicode (UTF-16) to multi-byte (local codepage) manually. Prior to this change we used FormatMessageW + conversion because some Windows CE did not have FormatMessageA. Since curl no longer supports Windows CE, FormatMessageA can be used. Closes https://github.com/curl/curl/pull/20261 --- lib/curlx/winapi.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/curlx/winapi.c b/lib/curlx/winapi.c index b46a5bf0dd..a766a558d9 100644 --- a/lib/curlx/winapi.c +++ b/lib/curlx/winapi.c @@ -39,30 +39,27 @@ const char *curlx_get_winapi_error(DWORD err, char *buf, size_t buflen) { char *p; - wchar_t wbuf[256]; - DWORD wlen; if(!buflen) return NULL; - *buf = '\0'; - *wbuf = L'\0'; - /* We return the local codepage version of the error string because if it is output to the user's terminal it will likely be with functions which expect the local codepage (eg fprintf, failf, infof). */ - wlen = FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err, - LANG_NEUTRAL, wbuf, CURL_ARRAYSIZE(wbuf), NULL); - if(wlen && !wcstombs_s(NULL, buf, buflen, wbuf, wlen)) { - /* Truncate multiple lines */ - p = strchr(buf, '\n'); - if(p) { - if(p > buf && *(p - 1) == '\r') - *(p - 1) = '\0'; - else - *p = '\0'; - } + if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err, + LANG_NEUTRAL, buf, (DWORD)buflen, NULL)) { + *buf = '\0'; + return NULL; + } + + /* Truncate multiple lines */ + p = strchr(buf, '\n'); + if(p) { + if(p > buf && *(p - 1) == '\r') + *(p - 1) = '\0'; + else + *p = '\0'; } return *buf ? buf : NULL;