diff options
author | Pete Batard <pbatard@gmail.com> | 2010-08-02 12:40:24 +0100 |
---|---|---|
committer | Pete Batard <pbatard@gmail.com> | 2010-08-02 12:40:24 +0100 |
commit | c9b0a3b82e3c7771e1f34c134578f9d9bfd71b2e (patch) | |
tree | a009c45441324cdc1b2dd5396208ae6cc4ada97c | |
parent | 8b5f31e56836e5ff3bcbb23b0ffdef596eb2ab93 (diff) | |
download | libusb-c9b0a3b82e3c7771e1f34c134578f9d9bfd71b2e.tar.gz |
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
-rw-r--r-- | libusb/os/windows_usb.c | 12 | ||||
-rw-r--r-- | 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; j<root_size; j++) @@ -1273,7 +1273,7 @@ enum libusb_hid_report_type { } for (j=0; j<nb_paths; j++) { - if ( (safe_strncmp(sanitized_path[j], sanitized_short, strlen(sanitized_short)) == 0) + if ( (safe_strncmp(sanitized_path[j], sanitized_short, safe_strlen(sanitized_short)) == 0) || (safe_strcmp(hid_path[j], sanitized_short) == 0 ) ) { // HID devices can have multiple collections (COL##) for each MI_## interface if (priv->usb_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)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2)) #define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<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<strlen(str); i++) + for (i=0; i<safe_strlen(str); i++) str[i] = (char)toupper((int)str[i]); } @@ -315,7 +315,7 @@ struct windows_transfer_priv { * API macros - from libusb-win32 1.x */ #define DLL_DECLARE(api, ret, name, args) \ - typedef ret (api * __dll_##name##_t)args; __dll_##name##_t name + typedef ret (api * __dll_##name##_t)args; __dll_##name##_t name = NULL #define DLL_LOAD(dll, name, ret_on_failure) \ do { \ @@ -343,7 +343,7 @@ struct windows_transfer_priv { typedef DWORD DEVNODE, DEVINST; typedef DEVNODE *PDEVNODE, *PDEVINST; typedef DWORD RETURN_TYPE; -typedef RETURN_TYPE CONFIGRET; +typedef RETURN_TYPE CONFIGRET; #define CR_SUCCESS 0x00000000 #define CR_NO_SUCH_DEVNODE 0x0000000D |