diff options
author | Chris Dickens <christopher.a.dickens@gmail.com> | 2017-01-12 14:07:02 -0800 |
---|---|---|
committer | Chris Dickens <christopher.a.dickens@gmail.com> | 2017-01-12 15:26:40 -0800 |
commit | d113986615d22632c4c8ceb941176ece7e4cfedf (patch) | |
tree | bfbfff0b31723f323a9551afbd64836fb4f05d7d /libusb | |
parent | e37962295294ab8f97cb67171f6862f4d07cbfdd (diff) | |
download | libusb-d113986615d22632c4c8ceb941176ece7e4cfedf.tar.gz |
Windows/WinCE: Improvements to windows_error_str() function
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>
Diffstat (limited to 'libusb')
-rw-r--r-- | libusb/core.c | 4 | ||||
-rw-r--r-- | libusb/os/wince_usb.c | 37 | ||||
-rw-r--r-- | libusb/os/windows_nt_common.c | 36 | ||||
-rw-r--r-- | libusb/os/windows_nt_common.h | 2 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
5 files changed, 42 insertions, 39 deletions
diff --git a/libusb/core.c b/libusb/core.c index be32117..d45bfe1 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -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)); diff --git a/libusb/os/wince_usb.c b/libusb/os/wince_usb.c index bb933a7..22ed669 100644 --- a/libusb/os/wince_usb.c +++ b/libusb/os/wince_usb.c @@ -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; } diff --git a/libusb/os/windows_nt_common.c b/libusb/os/windows_nt_common.c index 8dcbd9b..bbb61fc 100644 --- a/libusb/os/windows_nt_common.c +++ b/libusb/os/windows_nt_common.c @@ -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; diff --git a/libusb/os/windows_nt_common.h b/libusb/os/windows_nt_common.h index 9749e00..52ea870 100644 --- a/libusb/os/windows_nt_common.h +++ b/libusb/os/windows_nt_common.h @@ -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 diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 1784094..6ff4731 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11173 +#define LIBUSB_NANO 11174 |