Windows/WinCE: Improvements to windows_error_str() function
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 12 Jan 2017 22:07:02 +0000 (14:07 -0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 12 Jan 2017 23:26:40 +0000 (15:26 -0800)
1) Add FORMAT_MESSAGE_IGNORE_INSERTS to flags for security.
2) Optimize removal of CR/LF terminators
3) Switch to use snprintf()

Windows-specific:
  1) Don't waste time converting debug messages to unicode

WinCE-specific:
  1) Get rid of TCHAR code

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/core.c
libusb/os/wince_usb.c
libusb/os/windows_nt_common.c
libusb/os/windows_nt_common.h
libusb/version_nano.h

index be32117..d45bfe1 100644 (file)
@@ -2307,7 +2307,9 @@ static void usbi_log_str(struct libusb_context *ctx,
        enum libusb_log_level level, const char * str)
 {
 #if defined(USE_SYSTEM_LOGGING_FACILITY)
-#if defined(OS_WINDOWS) || defined(OS_WINCE)
+#if defined(OS_WINDOWS)
+       OutputDebugString(str);
+#elif defined(OS_WINCE)
        /* Windows CE only supports the Unicode version of OutputDebugString. */
        WCHAR wbuf[USBI_MAX_LOG_LEN];
        MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf));
index bb933a7..22ed669 100644 (file)
@@ -41,38 +41,39 @@ static int concurrent_usage = -1;
  * uses retval as errorcode, or, if 0, use GetLastError()
  */
 #if defined(ENABLE_LOGGING)
-static const char *windows_error_str(DWORD retval)
+static const char *windows_error_str(DWORD error_code)
 {
        static TCHAR wErr_string[ERR_BUFFER_SIZE];
        static char err_string[ERR_BUFFER_SIZE];
 
-       DWORD error_code, format_error;
        DWORD size;
-       size_t i;
+       int len;
 
-       error_code = retval ? retval : GetLastError();
+       if (error_code == 0)
+               error_code = GetLastError();
 
-       safe_stprintf(wErr_string, ERR_BUFFER_SIZE, _T("[%u] "), (unsigned int)error_code);
+       len = sprintf(err_string, "[%u] ", (unsigned int)error_code);
 
-       size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
-               MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &wErr_string[safe_tcslen(wErr_string)],
-               ERR_BUFFER_SIZE - (DWORD)safe_tcslen(wErr_string), NULL);
+       size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
+               NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+               wErr_string, ERR_BUFFER_SIZE, NULL);
        if (size == 0) {
-               format_error = GetLastError();
+               DWORD format_error = GetLastError();
                if (format_error)
-                       safe_stprintf(wErr_string, ERR_BUFFER_SIZE,
-                               _T("Windows error code %u (FormatMessage error code %u)"),
+                       snprintf(err_string, ERR_BUFFER_SIZE,
+                               "Windows error code %u (FormatMessage error code %u)",
                                (unsigned int)error_code, (unsigned int)format_error);
                else
-                       safe_stprintf(wErr_string, ERR_BUFFER_SIZE, _T("Unknown error code %u"), (unsigned int)error_code);
+                       snprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
        } else {
-               // Remove CR/LF terminators
-               for (i = safe_tcslen(wErr_string) - 1; ((wErr_string[i] == 0x0A) || (wErr_string[i] == 0x0D)); i--)
-                       wErr_string[i] = 0;
-       }
+               // Remove CR/LF terminators, if present
+               size_t pos = size - 2;
+               if (wErr_string[pos] == 0x0D)
+                       wErr_string[pos] = 0;
 
-       if (WideCharToMultiByte(CP_ACP, 0, wErr_string, -1, err_string, ERR_BUFFER_SIZE, NULL, NULL) < 0)
-               strcpy(err_string, "Unable to convert error string");
+               if (!WideCharToMultiByte(CP_ACP, 0, wErr_string, -1, &err_string[len], ERR_BUFFER_SIZE - len, NULL, NULL))
+                       strcpy(err_string, "Unable to convert error string");
+       }
 
        return err_string;
 }
index 8dcbd9b..bbb61fc 100644 (file)
@@ -63,17 +63,17 @@ static unsigned __stdcall windows_clock_gettime_threaded(void *param);
 * uses retval as errorcode, or, if 0, use GetLastError()
 */
 #if defined(ENABLE_LOGGING)
-const char *windows_error_str(DWORD retval)
+const char *windows_error_str(DWORD error_code)
 {
        static char err_string[ERR_BUFFER_SIZE];
 
-       DWORD error_code, format_error;
        DWORD size;
-       ssize_t i;
+       int len;
 
-       error_code = retval ? retval : GetLastError();
+       if (error_code == 0)
+               error_code = GetLastError();
 
-       safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%u] ", (unsigned int)error_code);
+       len = sprintf(err_string, "[%u] ", (unsigned int)error_code);
 
        // Translate codes returned by SetupAPI. The ones we are dealing with are either
        // in 0x0000xxxx or 0xE000xxxx and can be distinguished from standard error codes.
@@ -89,22 +89,22 @@ const char *windows_error_str(DWORD retval)
                break;
        }
 
-       size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
-                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)],
-                       ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL);
+       size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
+                       NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                       &err_string[len], ERR_BUFFER_SIZE - len, NULL);
        if (size == 0) {
-               format_error = GetLastError();
+               DWORD format_error = GetLastError();
                if (format_error)
-                       safe_sprintf(err_string, ERR_BUFFER_SIZE,
-                                       "Windows error code %u (FormatMessage error code %u)",
-                                       (unsigned int)error_code, (unsigned int)format_error);
+                       snprintf(err_string, ERR_BUFFER_SIZE,
+                               "Windows error code %u (FormatMessage error code %u)",
+                               (unsigned int)error_code, (unsigned int)format_error);
                else
-                       safe_sprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
-       }
-       else {
-               // Remove CR/LF terminators
-               for (i = safe_strlen(err_string) - 1; (i >= 0) && ((err_string[i] == 0x0A) || (err_string[i] == 0x0D)); i--)
-                       err_string[i] = 0;
+                       snprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
+       } else {
+               // Remove CRLF from end of message, if present
+               size_t pos = len + size - 2;
+               if (err_string[pos] == '\r')
+                       err_string[pos] = '\0';
        }
 
        return err_string;
index 9749e00..52ea870 100644 (file)
@@ -59,5 +59,5 @@ void windows_handle_callback(struct usbi_transfer *itransfer, uint32_t io_result
 int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
 
 #if defined(ENABLE_LOGGING)
-const char *windows_error_str(DWORD retval);
+const char *windows_error_str(DWORD error_code);
 #endif
index 1784094..6ff4731 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11173
+#define LIBUSB_NANO 11174