From c9b0a3b82e3c7771e1f34c134578f9d9bfd71b2e Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Mon, 2 Aug 2010 12:40:24 +0100 Subject: minor improvements cast pointer to void* for safe_free always use safe_strlen in lieu of strlen avoid the use of a strlen parameter in a macro don't feed negative values to min() in safe_strncat set uninitialized DLL functions to NULL --- libusb/os/windows_usb.c | 12 ++++++------ libusb/os/windows_usb.h | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index 88d3cc0..69ea185 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -198,7 +198,7 @@ static char err_string[ERR_BUFFER_SIZE]; safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%d] ", errcode); size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &err_string[strlen(err_string)], + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &err_string[safe_strlen(err_string)], ERR_BUFFER_SIZE, NULL); if (size == 0) { @@ -226,7 +226,7 @@ static char* sanitize_path(const char* path) if (path == NULL) return NULL; - size = strlen(path)+1; + size = safe_strlen(path)+1; root_size = sizeof(root_prefix)-1; // Microsoft indiscriminatly uses '\\?\', '\\.\', '##?#" or "##.#" for root prefixes. @@ -239,7 +239,7 @@ static char* sanitize_path(const char* path) if ((ret_path = (char*)calloc(size, 1)) == NULL) return NULL; - safe_strncpy(&ret_path[add_root], size-add_root, path, strlen(path)); + safe_strncpy(&ret_path[add_root], size-add_root, path, safe_strlen(path)); // Ensure consistancy with root prefix for (j=0; jusb_interface[interface_number].path != NULL) { @@ -1369,7 +1369,7 @@ static int set_hid_device(struct libusb_context *ctx, struct windows_device_priv } // NB: we compare strings of different lengths below => strncmp - if (safe_strncmp(priv->path, sanitized_path, strlen(sanitized_path)) == 0) { + if (safe_strncmp(priv->path, sanitized_path, safe_strlen(sanitized_path)) == 0) { priv->usb_interface[interface_number].path = sanitize_path(dev_interface_details->DevicePath); priv->usb_interface[interface_number].apib = &usb_api_backend[USB_API_HID]; usbi_dbg("interface_path[%d]: %s", interface_number, priv->usb_interface[interface_number].path); @@ -1471,7 +1471,7 @@ static int set_device_paths(struct libusb_context *ctx, struct discovered_devs * parent_priv = __device_priv(priv->parent_dev); // NB: we compare strings of different lengths below => strncmp - if ( (safe_strncmp(parent_priv->path, sanitized_path, strlen(sanitized_path)) == 0) + if ( (safe_strncmp(parent_priv->path, sanitized_path, safe_strlen(sanitized_path)) == 0) && (port_nr == priv->connection_index) ) { priv->path = sanitize_path(dev_interface_details->DevicePath); diff --git a/libusb/os/windows_usb.h b/libusb/os/windows_usb.h index f8fcebc..99a3a94 100644 --- a/libusb/os/windows_usb.h +++ b/libusb/os/windows_usb.h @@ -65,22 +65,22 @@ extern char *_strdup(const char *strSource); // _beginthreadex is MSVCRT => unavailable for cygwin. Fallback to using CreateThread #define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, (LPTHREAD_START_ROUTINE)c, d, e, f) #endif -#define safe_free(p) do {if (p != NULL) {free(p); p = NULL;}} while(0) +#define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0) #define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0) #define safe_strncpy(dst, dst_max, src, count) do {strncpy(dst, src, dst_max); ((char*)dst)[dst_max-1] = 0;} while(0) -#define safe_strcpy(dst, dst_max, src) safe_strncpy(dst, dst_max, src, strlen(src)+1) -#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, min(count, dst_max - strlen(dst) - 1)) -#define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, strlen(src)+1) +#define safe_strcpy(dst, dst_max, src) safe_strncpy(dst, dst_max, src, safe_strlen(src)+1) +#define safe_strncat(dst, dst_max, src, count) strncat(dst, src, min((size_t)count, (size_t)(dst_max - safe_strlen(dst) - 1))) +#define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1) #define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2)) #define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"":str1), ((str2==NULL)?"":str2), count) #define safe_strlen(str) ((str==NULL)?0:strlen(str)) #define safe_sprintf _snprintf #define safe_unref_device(dev) do {if (dev != NULL) {libusb_unref_device(dev); dev = NULL;}} while(0) -#define wchar_to_utf8_ms(wstr, str, strlen) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, strlen, NULL, NULL) +#define wchar_to_utf8_ms(wstr, str, len) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL) inline void upperize(char* str) { size_t i; if (str == NULL) return; - for (i=0; i