]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
strerror: Fix an error looking up some Windows error strings
authorJay Satiro <raysatiro@yahoo.com>
Sat, 9 Nov 2019 23:07:59 +0000 (18:07 -0500)
committerJay Satiro <raysatiro@yahoo.com>
Sat, 9 Nov 2019 23:07:59 +0000 (18:07 -0500)
- Use FORMAT_MESSAGE_IGNORE_INSERTS to ignore format specifiers in
  Windows error strings.

Since we are not in control of the error code we don't know what
information may be needed by the error string's format specifiers.

Prior to this change Windows API error strings which contain specifiers
(think specifiers like similar to printf specifiers) would not be shown.
The FormatMessage Windows API call which turns a Windows error code into
a string could fail and set error ERROR_INVALID_PARAMETER if that error
string contained a format specifier. FormatMessage expects a va_list for
the specifiers, unless inserts are ignored in which case no substitution
is attempted.

Ref: https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353

lib/strerror.c
tests/server/util.c

index d0650d80c5ea0026b232d1e93740d7fdbebcf1e0..7aaa9f4a79903e1e15d09b1472e2cd4d09ebb6fd 100644 (file)
@@ -670,7 +670,8 @@ const char *Curl_strerror(int err, char *buf, size_t buflen)
     wchar_t wbuf[256];
     wbuf[0] = L'\0';
 
-    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
+    FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
+                   FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
                   LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
     wcstombs(buf, wbuf, max);
   }
@@ -680,7 +681,8 @@ const char *Curl_strerror(int err, char *buf, size_t buflen)
     strncpy(buf, strerror(err), max);
   else {
     if(!get_winsock_error(err, buf, max) &&
-       !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
+       !FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM |
+                        FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
                        LANG_NEUTRAL, buf, (DWORD)max, NULL))
       msnprintf(buf, max, "Unknown error %d (%#x)", err, err);
   }
index cc53d3bf4f750c67c1fd42ecc3269b7ea25f5af1..263f0cece322744061337e55ce0d7d9df053fb28 100644 (file)
@@ -150,7 +150,8 @@ void win32_perror(const char *msg)
   char buf[512];
   DWORD err = SOCKERRNO;
 
-  if(!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
+  if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM |
+                      FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
                      LANG_NEUTRAL, buf, sizeof(buf), NULL))
     msnprintf(buf, sizeof(buf), "Unknown error %lu (%#lx)", err, err);
   if(msg)