]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
strerror: Revert to local codepage for Windows error string
authorJay Satiro <raysatiro@yahoo.com>
Tue, 13 Oct 2020 07:22:55 +0000 (03:22 -0400)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 13 Oct 2020 12:17:27 +0000 (14:17 +0200)
- Change get_winapi_error() to return the error string in the local
  codepage instead of UTF-8 encoding.

Two weeks ago bed5f84 fixed get_winapi_error() to work on xbox, but it
also changed the error string's encoding from local codepage to UTF-8.

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).

This is essentially a partial revert of bed5f84. The support for xbox
remains but the error string is reverted back to local codepage.

Ref: https://github.com/curl/curl/pull/6005

Reviewed-by: Marcel Raad
Closes #6065

lib/strerror.c

index 0a3b73844c3ff4b19e5bc4a396933750aa70b17c..b5808df2d12a430ebf978ffff192c26d39b9bf84 100644 (file)
@@ -44,7 +44,6 @@
 #endif
 
 #include "strerror.h"
-#include "curl_multibyte.h"
 /* The last 3 #include files should be in this order */
 #include "curl_printf.h"
 #include "curl_memory.h"
@@ -656,26 +655,26 @@ static const char *
 get_winapi_error(int err, char *buf, size_t buflen)
 {
   char *p;
+  wchar_t wbuf[256];
 
   if(!buflen)
     return NULL;
 
   *buf = '\0';
-
-  {
-    TCHAR wbuf[256];
-    wbuf[0] = L'\0';
-
-    if(FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
-                      FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
-                     LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(TCHAR), NULL)) {
-      char *msg = curlx_convert_tchar_to_UTF8(wbuf);
-      if(msg) {
-        strncpy(buf, msg, buflen - 1);
-        buf[buflen-1] = '\0';
-        curlx_unicodefree(msg);
-      }
-    }
+  *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).
+     FormatMessageW -> wcstombs is used for Windows CE compatibility. */
+  if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
+                     FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
+                    LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
+    size_t written = wcstombs(buf, wbuf, buflen - 1);
+    if(written != (size_t)-1)
+      buf[written] = '\0';
+    else
+      *buf = '\0';
   }
 
   /* Truncate multiple lines */