]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
winapi: use FormatMessageA instead of FormatMessageW
authordEajL3kA <Cumpoing79@web.de>
Fri, 16 Jan 2026 10:20:48 +0000 (11:20 +0100)
committerJay Satiro <raysatiro@yahoo.com>
Wed, 21 Jan 2026 07:49:29 +0000 (02:49 -0500)
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

index b46a5bf0ddf0b4962111f412333155525355f59c..a766a558d968cfea56cb818390ffb6df5178a1d2 100644 (file)
 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;