From 7d33b9cca80f2f9862c633925986e3eb0420d3a3 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Wed, 2 Nov 2011 12:22:01 +0000 Subject: [windows] memory allocation improvements * add missing warning on alloc failure * prefer calloc() to malloc() * add casts to address VS2010 IntelliSense warnings --- libusb/os/poll_windows.c | 8 ++++---- libusb/os/threads_windows.c | 2 +- libusb/os/windows_usb.c | 27 ++++++++++++++------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/libusb/os/poll_windows.c b/libusb/os/poll_windows.c index 9d6f3e6..885534c 100644 --- a/libusb/os/poll_windows.c +++ b/libusb/os/poll_windows.c @@ -199,7 +199,7 @@ int _fd_to_index_and_lock(int fd) OVERLAPPED *create_overlapped(void) { - OVERLAPPED *overlapped = calloc(1, sizeof(OVERLAPPED)); + OVERLAPPED *overlapped = (OVERLAPPED*) calloc(1, sizeof(OVERLAPPED)); if (overlapped == NULL) { return NULL; } @@ -292,7 +292,7 @@ int usbi_pipe(int filedes[2]) CHECK_INIT_POLLING; - overlapped = calloc(1, sizeof(OVERLAPPED)); + overlapped = (OVERLAPPED*) calloc(1, sizeof(OVERLAPPED)); if (overlapped == NULL) { return -1; } @@ -590,8 +590,8 @@ int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout) CHECK_INIT_POLLING; triggered = 0; - handles_to_wait_on = malloc((nfds+1)*sizeof(HANDLE)); // +1 for fd_update - handle_to_index = malloc(nfds*sizeof(int)); + handles_to_wait_on = (HANDLE*) calloc(nfds+1, sizeof(HANDLE)); // +1 for fd_update + handle_to_index = (int*) calloc(nfds, sizeof(int)); if ((handles_to_wait_on == NULL) || (handle_to_index == NULL)) { errno = ENOMEM; triggered = -1; diff --git a/libusb/os/threads_windows.c b/libusb/os/threads_windows.c index 0edd6d1..ca600d0 100644 --- a/libusb/os/threads_windows.c +++ b/libusb/os/threads_windows.c @@ -156,7 +156,7 @@ static int __inline usbi_cond_intwait(usbi_cond_t *cond, } } if(!found) { - pos = malloc(sizeof(struct usbi_cond_perthread)); + pos = (struct usbi_cond_perthread*) calloc(1, sizeof(struct usbi_cond_perthread)); if(!pos) return ((errno=ENOMEM)); // This errno is not POSIX-allowed. pos->tid = tid; pos->event = CreateEvent(NULL, FALSE, FALSE, NULL); // auto-reset. diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index 0fb4d35..4b8cea8 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -228,7 +228,7 @@ static char* sanitize_path(const char* path) size += add_root; } - if ((ret_path = (char*)calloc(size, 1)) == NULL) + if ((ret_path = (char*) calloc(size, 1)) == NULL) return NULL; safe_strcpy(&ret_path[add_root], size-add_root, path); @@ -367,7 +367,7 @@ static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_co goto err_exit; } - if ((dev_interface_details = malloc(size)) == NULL) { + if ((dev_interface_details = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) calloc(size, 1)) == NULL) { usbi_err(ctx, "could not allocate interface data for index %u.", _index); goto err_exit; } @@ -436,7 +436,7 @@ static int htab_create(struct libusb_context *ctx, unsigned long nel) htab_filled = 0; // allocate memory and zero out. - htab_table = (htab_entry*)calloc(htab_size + 1, sizeof(htab_entry)); + htab_table = (htab_entry*) calloc(htab_size + 1, sizeof(htab_entry)); if (htab_table == NULL) { usbi_err(ctx, "could not allocate space for hash table"); return 0; @@ -541,7 +541,7 @@ static unsigned long htab_hash(char* str) // string (same hash, different string) at the same time is extremely low safe_free(htab_table[idx].str); htab_table[idx].used = hval; - htab_table[idx].str = malloc(safe_strlen(str)+1); + htab_table[idx].str = (char*) malloc(safe_strlen(str)+1); if (htab_table[idx].str == NULL) { usbi_err(NULL, "could not duplicate string for hash table"); usbi_mutex_unlock(&htab_write_mutex); @@ -611,7 +611,7 @@ static int windows_assign_endpoints(struct libusb_device_handle *dev_handle, int return LIBUSB_SUCCESS; } - priv->usb_interface[iface].endpoint = malloc(if_desc->bNumEndpoints); + priv->usb_interface[iface].endpoint = (uint8_t*) malloc(if_desc->bNumEndpoints); if (priv->usb_interface[iface].endpoint == NULL) { return LIBUSB_ERROR_NO_MEM; } @@ -640,7 +640,7 @@ static bool is_api_driver(char* driver, uint8_t api) size_t len = safe_strlen(driver); if (len == 0) return false; - tmp_str = calloc(len+1, 1); + tmp_str = (char*) calloc(len+1, 1); if (tmp_str == NULL) return false; memcpy(tmp_str, driver, len+1); tok = strtok(tmp_str, sep_str); @@ -922,7 +922,7 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle if (dev->num_configurations == 0) return LIBUSB_ERROR_INVALID_PARAM; - priv->config_descriptor = malloc(dev->num_configurations * sizeof(PUSB_CONFIGURATION_DESCRIPTOR)); + priv->config_descriptor = (unsigned char**) calloc(dev->num_configurations, sizeof(PUSB_CONFIGURATION_DESCRIPTOR)); if (priv->config_descriptor == NULL) return LIBUSB_ERROR_NO_MEM; for (i=0; inum_configurations; i++) @@ -960,7 +960,7 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle } size = sizeof(USB_DESCRIPTOR_REQUEST) + cd_buf_short.data.wTotalLength; - if ((cd_buf_actual = (PUSB_DESCRIPTOR_REQUEST)malloc(size)) == NULL) { + if ((cd_buf_actual = (PUSB_DESCRIPTOR_REQUEST) calloc(1, size)) == NULL) { usbi_err(ctx, "could not allocate configuration descriptor buffer for '%s'.", device_id); LOOP_BREAK(LIBUSB_ERROR_NO_MEM); } @@ -996,10 +996,9 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle i, cd_data->bConfigurationValue, cd_data->wTotalLength); // Cache the descriptor - priv->config_descriptor[i] = malloc(cd_data->wTotalLength); + priv->config_descriptor[i] = (unsigned char*) malloc(cd_data->wTotalLength); if (priv->config_descriptor[i] == NULL) return LIBUSB_ERROR_NO_MEM; - memcpy(priv->config_descriptor[i], cd_data, cd_data->wTotalLength); } return LIBUSB_SUCCESS; @@ -1262,7 +1261,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered guid[DEV_PASS] = &GUID_DEVINTERFACE_USB_DEVICE; nb_guids = DEV_PASS+1; - unref_list = malloc(unref_size*sizeof(libusb_device*)); + unref_list = (libusb_device**) calloc(unref_size, sizeof(libusb_device*)); if (unref_list == NULL) { return LIBUSB_ERROR_NO_MEM; } @@ -1384,7 +1383,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered usbi_err(ctx, "program assertion failed: too many GUIDs"); LOOP_BREAK(LIBUSB_ERROR_OVERFLOW); } - if_guid = calloc(1, sizeof(GUID)); + if_guid = (GUID*) calloc(1, sizeof(GUID)); pCLSIDFromString(guid_string_w, if_guid); guid[nb_guids++] = if_guid; usbi_dbg("extra GUID: %s", guid_to_string(if_guid)); @@ -1493,9 +1492,11 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered break; default: // For other devices, the first interface is the same as the device - priv->usb_interface[0].path = malloc(safe_strlen(priv->path)+1); + priv->usb_interface[0].path = (char*) calloc(safe_strlen(priv->path)+1, 1); if (priv->usb_interface[0].path != NULL) { safe_strcpy(priv->usb_interface[0].path, safe_strlen(priv->path)+1, priv->path); + } else { + usbi_warn(ctx, "could not duplicate interface path '%s'", priv->path); } // The following is needed if we want API calls to work for both simple // and composite devices. -- cgit v1.2.1