From: Sean McBride Date: Sat, 15 Dec 2018 22:08:34 +0000 (-0500) Subject: Fixed many compiler warnings about sign and size mismatch X-Git-Tag: upstream/1.0.23~53 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8a05a3f4e7424eb2071e4dbe1796cff3b660a141;p=platform%2Fupstream%2Flibusb.git Fixed many compiler warnings about sign and size mismatch - added various casts - added some asserts where the casts made assumptions - enabled additional warnings in Xcode project (especially -Wshorten-64-to-32) Closes #509 Signed-off-by: Nathan Hjelm --- diff --git a/Xcode/common.xcconfig b/Xcode/common.xcconfig index cc0ac23..bb97789 100644 --- a/Xcode/common.xcconfig +++ b/Xcode/common.xcconfig @@ -37,12 +37,21 @@ GCC_WARN_UNKNOWN_PRAGMAS = YES GCC_WARN_UNUSED_FUNCTION = YES GCC_WARN_UNUSED_LABEL = YES GCC_WARN_UNUSED_VARIABLE = YES +GCC_WARN_UNUSED_PARAMETER = YES CLANG_WARN_EMPTY_BODY = YES CLANG_WARN_CONSTANT_CONVERSION = YES CLANG_WARN_ENUM_CONVERSION = YES CLANG_WARN_INT_CONVERSION = YES CLANG_WARN_DOCUMENTATION_COMMENTS = YES CLANG_WARN_BOOL_CONVERSION = YES +CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES +CLANG_WARN_FLOAT_CONVERSION = YES +CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES +CLANG_WARN_INFINITE_RECURSION = YES +CLANG_WARN_ASSIGN_ENUM = YES +CLANG_WARN_STRICT_PROTOTYPES = YES +CLANG_WARN_COMMA = YES +CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES // Static analyzer warnings. CLANG_ANALYZER_SECURITY_FLOATLOOPCOUNTER = YES diff --git a/examples/ezusb.c b/examples/ezusb.c index e3b488d..4fe9e65 100644 --- a/examples/ezusb.c +++ b/examples/ezusb.c @@ -299,7 +299,7 @@ static int parse_ihex(FILE *image, void *context, /* Read the target offset (address up to 64KB) */ tmp = buf[7]; buf[7] = 0; - off = (int)strtoul(buf+3, NULL, 16); + off = (unsigned int)strtoul(buf+3, NULL, 16); buf[7] = tmp; /* Initialize data_addr */ diff --git a/examples/sam3u_benchmark.c b/examples/sam3u_benchmark.c index 7189b22..43d286a 100644 --- a/examples/sam3u_benchmark.c +++ b/examples/sam3u_benchmark.c @@ -55,11 +55,11 @@ static void LIBUSB_CALL cb_xfr(struct libusb_transfer *xfr) struct libusb_iso_packet_descriptor *pack = &xfr->iso_packet_desc[i]; if (pack->status != LIBUSB_TRANSFER_COMPLETED) { - fprintf(stderr, "Error: pack %u status %d\n", i, pack->status); + fprintf(stderr, "Error: pack %d status %d\n", i, pack->status); exit(5); } - printf("pack%u length:%u, actual_length:%u\n", i, pack->length, pack->actual_length); + printf("pack%d length:%u, actual_length:%u\n", i, pack->length, pack->actual_length); } } diff --git a/examples/xusb.c b/examples/xusb.c index 4ac11b8..966d8c1 100644 --- a/examples/xusb.c +++ b/examples/xusb.c @@ -627,7 +627,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in) } display_buffer_hex(hid_report_descriptor, descriptor_size); if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) { - if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != descriptor_size) { + if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != (size_t)descriptor_size) { printf(" Error writing descriptor to file\n"); } fclose(fd); diff --git a/libusb/core.c b/libusb/core.c index f839b6a..2d3bf5b 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -845,8 +845,8 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx, } /* convert discovered_devs into a list */ - len = discdevs->len; - ret = calloc(len + 1, sizeof(struct libusb_device *)); + len = (ssize_t)discdevs->len; + ret = calloc((size_t)len + 1, sizeof(struct libusb_device *)); if (!ret) { len = LIBUSB_ERROR_NO_MEM; goto out; @@ -1680,12 +1680,12 @@ int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev_handle, return LIBUSB_ERROR_NO_DEVICE; usbi_mutex_lock(&dev_handle->lock); - if (dev_handle->claimed_interfaces & (1 << interface_number)) + if (dev_handle->claimed_interfaces & (1U << interface_number)) goto out; r = usbi_backend.claim_interface(dev_handle, interface_number); if (r == 0) - dev_handle->claimed_interfaces |= 1 << interface_number; + dev_handle->claimed_interfaces |= 1U << interface_number; out: usbi_mutex_unlock(&dev_handle->lock); @@ -1721,14 +1721,14 @@ int API_EXPORTED libusb_release_interface(libusb_device_handle *dev_handle, return LIBUSB_ERROR_INVALID_PARAM; usbi_mutex_lock(&dev_handle->lock); - if (!(dev_handle->claimed_interfaces & (1 << interface_number))) { + if (!(dev_handle->claimed_interfaces & (1U << interface_number))) { r = LIBUSB_ERROR_NOT_FOUND; goto out; } r = usbi_backend.release_interface(dev_handle, interface_number); if (r == 0) - dev_handle->claimed_interfaces &= ~(1 << interface_number); + dev_handle->claimed_interfaces &= ~(1U << interface_number); out: usbi_mutex_unlock(&dev_handle->lock); @@ -1770,7 +1770,7 @@ int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_hand return LIBUSB_ERROR_NO_DEVICE; } - if (!(dev_handle->claimed_interfaces & (1 << interface_number))) { + if (!(dev_handle->claimed_interfaces & (1U << interface_number))) { usbi_mutex_unlock(&dev_handle->lock); return LIBUSB_ERROR_NOT_FOUND; } @@ -2507,7 +2507,7 @@ static void usbi_log_str(enum libusb_log_level level, const char *str) case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break; } syslog(syslog_level, "%s", str); -#else /* All of gcc, Clang, XCode seem to use #warning */ +#else /* All of gcc, Clang, Xcode seem to use #warning */ #warning System logging is not supported on this platform. Logging to stderr will be used instead. fputs(str, stderr); #endif @@ -2589,8 +2589,8 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level, if (global_debug) { header_len = snprintf(buf, sizeof(buf), - "[%2d.%06d] [%08x] libusb: %s [%s] ", - (int)now.tv_sec, (int)(now.tv_nsec / 1000L), usbi_get_tid(), prefix, function); + "[%2ld.%06ld] [%08x] libusb: %s [%s] ", + (long)now.tv_sec, (long)(now.tv_nsec / 1000L), usbi_get_tid(), prefix, function); } else { header_len = snprintf(buf, sizeof(buf), "libusb: %s [%s] ", prefix, function); @@ -2603,16 +2603,16 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level, } /* Make sure buffer is NUL terminated */ buf[header_len] = '\0'; - text_len = vsnprintf(buf + header_len, sizeof(buf) - header_len, + text_len = vsnprintf(buf + header_len, sizeof(buf) - (size_t)header_len, format, args); if (text_len < 0 || text_len + header_len >= (int)sizeof(buf)) { /* Truncated log output. On some platforms a -1 return value means * that the output was truncated. */ - text_len = sizeof(buf) - header_len; + text_len = (int)sizeof(buf) - header_len; } - if (header_len + text_len + sizeof(USBI_LOG_LINE_END) >= sizeof(buf)) { + if (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END) >= (int)sizeof(buf)) { /* Need to truncate the text slightly to fit on the terminator. */ - text_len -= (header_len + text_len + sizeof(USBI_LOG_LINE_END)) - sizeof(buf); + text_len -= (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END)) - (int)sizeof(buf); } strcpy(buf + header_len + text_len, USBI_LOG_LINE_END); diff --git a/libusb/descriptor.c b/libusb/descriptor.c index 74d6de5..7ee3090 100644 --- a/libusb/descriptor.c +++ b/libusb/descriptor.c @@ -62,7 +62,7 @@ int usbi_parse_descriptor(const unsigned char *source, const char *descriptor, if (host_endian) { memcpy(dp, sp, 2); } else { - w = (sp[1] << 8) | sp[0]; + w = (uint16_t)((sp[1] << 8) | sp[0]); *((uint16_t *)dp) = w; } sp += 2; @@ -74,8 +74,8 @@ int usbi_parse_descriptor(const unsigned char *source, const char *descriptor, if (host_endian) { memcpy(dp, sp, 4); } else { - d = (sp[3] << 24) | (sp[2] << 16) | - (sp[1] << 8) | sp[0]; + d = (uint32_t)((sp[3] << 24) | (sp[2] << 16) | + (sp[1] << 8) | sp[0]); *((uint32_t *)dp) = d; } sp += 4; @@ -168,13 +168,13 @@ static int parse_endpoint(struct libusb_context *ctx, /* Copy any unknown descriptors into a storage area for drivers */ /* to later parse */ len = (int)(buffer - begin); - if (!len) { + if (len <= 0) { endpoint->extra = NULL; endpoint->extra_length = 0; return parsed; } - extra = malloc(len); + extra = malloc((size_t)len); endpoint->extra = extra; if (!extra) { endpoint->extra_length = 0; @@ -230,7 +230,7 @@ static int parse_interface(libusb_context *ctx, (struct libusb_interface_descriptor *) usb_interface->altsetting; altsetting = usbi_reallocf(altsetting, sizeof(struct libusb_interface_descriptor) * - (usb_interface->num_altsetting + 1)); + ((size_t)usb_interface->num_altsetting + 1)); if (!altsetting) { r = LIBUSB_ERROR_NO_MEM; goto err; @@ -307,8 +307,8 @@ static int parse_interface(libusb_context *ctx, /* Copy any unknown descriptors into a storage area for */ /* drivers to later parse */ len = (int)(buffer - begin); - if (len) { - ifp->extra = malloc(len); + if (len > 0) { + ifp->extra = malloc((size_t)len); if (!ifp->extra) { r = LIBUSB_ERROR_NO_MEM; goto err; @@ -453,10 +453,10 @@ static int parse_configuration(struct libusb_context *ctx, /* Copy any unknown descriptors into a storage area for */ /* drivers to later parse */ len = (int)(buffer - begin); - if (len) { + if (len > 0) { /* FIXME: We should realloc and append here */ if (!config->extra_length) { - config->extra = malloc(len); + config->extra = malloc((size_t)len); if (!config->extra) { r = LIBUSB_ERROR_NO_MEM; goto err; @@ -1163,7 +1163,7 @@ int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev_ha if (r < 4) return LIBUSB_ERROR_IO; - langid = tbuf[2] | (tbuf[3] << 8); + langid = (uint16_t)(tbuf[2] | (tbuf[3] << 8)); r = libusb_get_string_descriptor(dev_handle, desc_index, langid, tbuf, sizeof(tbuf)); diff --git a/libusb/hotplug.h b/libusb/hotplug.h index dbadbcb..75b2695 100644 --- a/libusb/hotplug.h +++ b/libusb/hotplug.h @@ -36,16 +36,16 @@ enum usbi_hotplug_flags { */ /* The vendor_id field is valid for matching */ - USBI_HOTPLUG_VENDOR_ID_VALID = (1 << 3), + USBI_HOTPLUG_VENDOR_ID_VALID = (1U << 3), /* The product_id field is valid for matching */ - USBI_HOTPLUG_PRODUCT_ID_VALID = (1 << 4), + USBI_HOTPLUG_PRODUCT_ID_VALID = (1U << 4), /* The dev_class field is valid for matching */ - USBI_HOTPLUG_DEV_CLASS_VALID = (1 << 5), + USBI_HOTPLUG_DEV_CLASS_VALID = (1U << 5), /* This callback has been unregistered and needs to be freed */ - USBI_HOTPLUG_NEEDS_FREE = (1 << 6), + USBI_HOTPLUG_NEEDS_FREE = (1U << 6), }; /** \ingroup hotplug diff --git a/libusb/io.c b/libusb/io.c index 6051d7f..4c29046 100644 --- a/libusb/io.c +++ b/libusb/io.c @@ -1248,7 +1248,7 @@ static int calculate_timeout(struct usbi_transfer *transfer) * use it on a non-isochronous endpoint. If you do this, ensure that at time * of submission, num_iso_packets is 0 and that type is set appropriately. * - * \param iso_packets number of isochronous packet descriptors to allocate + * \param iso_packets number of isochronous packet descriptors to allocate. Must be non-negative. * \returns a newly allocated transfer, or NULL on error */ DEFAULT_VISIBILITY @@ -1256,12 +1256,18 @@ struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer( int iso_packets) { struct libusb_transfer *transfer; - size_t os_alloc_size = usbi_backend.transfer_priv_size; - size_t alloc_size = sizeof(struct usbi_transfer) + size_t os_alloc_size; + size_t alloc_size; + struct usbi_transfer *itransfer; + + assert(iso_packets >= 0); + + os_alloc_size = usbi_backend.transfer_priv_size; + alloc_size = sizeof(struct usbi_transfer) + sizeof(struct libusb_transfer) - + (sizeof(struct libusb_iso_packet_descriptor) * iso_packets) + + (sizeof(struct libusb_iso_packet_descriptor) * (size_t)iso_packets) + os_alloc_size; - struct usbi_transfer *itransfer = calloc(1, alloc_size); + itransfer = calloc(1, alloc_size); if (!itransfer) return NULL; diff --git a/libusb/libusb.h b/libusb/libusb.h index 1229827..8a6b0bf 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -577,7 +577,7 @@ struct libusb_endpoint_descriptor { * it will store them here, should you wish to parse them. */ const unsigned char *extra; - /** Length of the extra descriptors, in bytes. */ + /** Length of the extra descriptors, in bytes. Must be non-negative. */ int extra_length; }; @@ -627,7 +627,7 @@ struct libusb_interface_descriptor { * it will store them here, should you wish to parse them. */ const unsigned char *extra; - /** Length of the extra descriptors, in bytes. */ + /** Length of the extra descriptors, in bytes. Must be non-negative. */ int extra_length; }; @@ -639,7 +639,8 @@ struct libusb_interface { * by the num_altsetting field. */ const struct libusb_interface_descriptor *altsetting; - /** The number of alternate settings that belong to this interface */ + /** The number of alternate settings that belong to this interface. + * Must be non-negative. */ int num_altsetting; }; @@ -686,7 +687,7 @@ struct libusb_config_descriptor { * descriptors, it will store them here, should you wish to parse them. */ const unsigned char *extra; - /** Length of the extra descriptors, in bytes. */ + /** Length of the extra descriptors, in bytes. Must be non-negative. */ int extra_length; }; @@ -1134,19 +1135,19 @@ enum libusb_transfer_status { * libusb_transfer.flags values */ enum libusb_transfer_flags { /** Report short frames as errors */ - LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, + LIBUSB_TRANSFER_SHORT_NOT_OK = 1U << 0, /** Automatically free() transfer buffer during libusb_free_transfer(). * Note that buffers allocated with libusb_dev_mem_alloc() should not * be attempted freed in this way, since free() is not an appropriate * way to release such memory. */ - LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, + LIBUSB_TRANSFER_FREE_BUFFER = 1U << 1, /** Automatically call libusb_free_transfer() after callback returns. * If this flag is set, it is illegal to call libusb_free_transfer() * from your transfer callback, as this will result in a double-free * when this flag is acted upon. */ - LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2, + LIBUSB_TRANSFER_FREE_TRANSFER = 1U << 2, /** Terminate transfers that are a multiple of the endpoint's * wMaxPacketSize with an extra zero length packet. This is useful @@ -1171,7 +1172,7 @@ enum libusb_transfer_flags { * * Available since libusb-1.0.9. */ - LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3, + LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1U << 3, }; /** \ingroup libusb_asyncio @@ -1232,7 +1233,7 @@ struct libusb_transfer { * to determine if errors occurred. */ enum libusb_transfer_status status; - /** Length of the data buffer */ + /** Length of the data buffer. Must be non-negative. */ int length; /** Actual length of data that was transferred. Read-only, and only for @@ -1251,7 +1252,7 @@ struct libusb_transfer { unsigned char *buffer; /** Number of isochronous packets. Only used for I/O with isochronous - * endpoints. */ + * endpoints. Must be non-negative. */ int num_iso_packets; /** Isochronous packet descriptors, for isochronous transfers only. */ @@ -1910,10 +1911,10 @@ typedef int libusb_hotplug_callback_handle; * Flags for hotplug events */ typedef enum { /** Default value when not using any flags. */ - LIBUSB_HOTPLUG_NO_FLAGS = 0, + LIBUSB_HOTPLUG_NO_FLAGS = 0U, /** Arm the callback and fire it for all matching currently attached devices. */ - LIBUSB_HOTPLUG_ENUMERATE = 1<<0, + LIBUSB_HOTPLUG_ENUMERATE = 1U << 0, } libusb_hotplug_flag; /** \ingroup libusb_hotplug @@ -1923,12 +1924,12 @@ typedef enum { * Hotplug events */ typedef enum { /** A device has been plugged in and is ready to use */ - LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01, + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01U, /** A device has left and is no longer available. * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device. * It is safe to call libusb_get_device_descriptor on a device that has left */ - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02, + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02U, } libusb_hotplug_event; /** \ingroup libusb_hotplug diff --git a/libusb/libusbi.h b/libusb/libusbi.h index c2ec479..d2adfeb 100644 --- a/libusb/libusbi.h +++ b/libusb/libusbi.h @@ -24,7 +24,7 @@ #include #include - +#include #include #include #include @@ -387,13 +387,13 @@ struct libusb_context { enum usbi_event_flags { /* The list of pollfds has been modified */ - USBI_EVENT_POLLFDS_MODIFIED = 1 << 0, + USBI_EVENT_POLLFDS_MODIFIED = 1U << 0, /* The user has interrupted the event handler */ - USBI_EVENT_USER_INTERRUPT = 1 << 1, + USBI_EVENT_USER_INTERRUPT = 1U << 1, /* A hotplug callback deregistration is pending */ - USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1 << 2, + USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2, }; /* Macros for managing event handling state */ @@ -495,24 +495,24 @@ struct usbi_transfer { enum usbi_transfer_state_flags { /* Transfer successfully submitted by backend */ - USBI_TRANSFER_IN_FLIGHT = 1 << 0, + USBI_TRANSFER_IN_FLIGHT = 1U << 0, /* Cancellation was requested via libusb_cancel_transfer() */ - USBI_TRANSFER_CANCELLING = 1 << 1, + USBI_TRANSFER_CANCELLING = 1U << 1, /* Operation on the transfer failed because the device disappeared */ - USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 2, + USBI_TRANSFER_DEVICE_DISAPPEARED = 1U << 2, }; enum usbi_transfer_timeout_flags { /* Set by backend submit_transfer() if the OS handles timeout */ - USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 0, + USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1U << 0, /* The transfer timeout has been handled */ - USBI_TRANSFER_TIMEOUT_HANDLED = 1 << 1, + USBI_TRANSFER_TIMEOUT_HANDLED = 1U << 1, /* The transfer timeout was successfully processed */ - USBI_TRANSFER_TIMED_OUT = 1 << 2, + USBI_TRANSFER_TIMED_OUT = 1U << 2, }; #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \ @@ -524,9 +524,10 @@ enum usbi_transfer_timeout_flags { static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer) { + assert(transfer->num_iso_packets >= 0); return ((unsigned char *)transfer) + sizeof(struct usbi_transfer) + sizeof(struct libusb_transfer) - + (transfer->num_iso_packets + + ((size_t)transfer->num_iso_packets * sizeof(struct libusb_iso_packet_descriptor)); } diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c index 71312d7..2d6b217 100644 --- a/libusb/os/darwin_usb.c +++ b/libusb/os/darwin_usb.c @@ -19,6 +19,7 @@ */ #include "config.h" +#include #include #include #include @@ -189,14 +190,14 @@ static int ep_to_pipeRef(struct libusb_device_handle *dev_handle, uint8_t ep, ui /* current interface */ struct darwin_interface *cInterface; - int8_t i, iface; + uint8_t i, iface; usbi_dbg ("converting ep address 0x%02x to pipeRef and interface", ep); for (iface = 0 ; iface < USB_MAXINTERFACES ; iface++) { cInterface = &priv->interfaces[iface]; - if (dev_handle->claimed_interfaces & (1 << iface)) { + if (dev_handle->claimed_interfaces & (1U << iface)) { for (i = 0 ; i < cInterface->num_endpoints ; i++) { if (cInterface->endpoint_addrs[i] == ep) { *pipep = i + 1; @@ -604,7 +605,8 @@ static int darwin_get_active_config_descriptor(struct libusb_device *dev, unsign if (config_index < 0) return config_index; - return darwin_get_config_descriptor (dev, config_index, buffer, len, host_endian); + assert(config_index >= 0 && config_index <= UINT8_MAX); + return darwin_get_config_descriptor (dev, (UInt8)config_index, buffer, len, host_endian); } static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian) { @@ -704,14 +706,16 @@ static enum libusb_error darwin_check_configuration (struct libusb_context *ctx, static IOReturn darwin_request_descriptor (usb_device_t **device, UInt8 desc, UInt8 desc_index, void *buffer, size_t buffer_size) { IOUSBDevRequestTO req; + assert(buffer_size <= UINT16_MAX); + memset (buffer, 0, buffer_size); /* Set up request for descriptor/ */ req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); req.bRequest = kUSBRqGetDescriptor; - req.wValue = desc << 8; + req.wValue = (UInt16)(desc << 8); req.wIndex = desc_index; - req.wLength = buffer_size; + req.wLength = (UInt16)buffer_size; req.pData = buffer; req.noDataTimeout = 20; req.completionTimeout = 100; @@ -721,7 +725,8 @@ static IOReturn darwin_request_descriptor (usb_device_t **device, UInt8 desc, UI static enum libusb_error darwin_cache_device_descriptor (struct libusb_context *ctx, struct darwin_cached_device *dev) { usb_device_t **device = dev->device; - int retries = 1, delay = 30000; + int retries = 1; + long delay = 30000; // microseconds int unsuspended = 0, try_unsuspend = 1, try_reconfigure = 1; int is_open = 0; IOReturn ret = 0, ret2; @@ -777,7 +782,7 @@ static enum libusb_error darwin_cache_device_descriptor (struct libusb_context * (void)(*device)->GetUSBDeviceInformation (device, &info); /* note that the device was suspended */ - if (info & (1 << kUSBInformationDeviceIsSuspendedBit) || 0 == info) + if (info & (1U << kUSBInformationDeviceIsSuspendedBit) || 0 == info) try_unsuspend = 1; #endif @@ -796,9 +801,9 @@ static enum libusb_error darwin_cache_device_descriptor (struct libusb_context * } if (kIOReturnSuccess != ret) { - usbi_dbg("kernel responded with code: 0x%08x. sleeping for %d ms before trying again", ret, delay/1000); + usbi_dbg("kernel responded with code: 0x%08x. sleeping for %ld ms before trying again", ret, delay/1000); /* sleep for a little while before trying again */ - nanosleep(&(struct timespec){delay / 1000000, (delay * 1000) % 1000000000UL}, NULL); + nanosleep(&(struct timespec){delay / 1000000, (delay * 1000) % 1000000000}, NULL); } } while (kIOReturnSuccess != ret && retries--); @@ -1011,7 +1016,8 @@ static enum libusb_error process_new_device (struct libusb_context *ctx, io_serv } dev->port_number = cached_device->port; dev->bus_number = cached_device->location >> 24; - dev->device_address = cached_device->address; + assert(cached_device->address <= UINT8_MAX); + dev->device_address = (uint8_t)cached_device->address; (*(priv->dev->device))->GetDeviceSpeed (priv->dev->device, &devSpeed); @@ -1131,7 +1137,7 @@ static void darwin_close (struct libusb_device_handle *dev_handle) { /* make sure all interfaces are released */ for (i = 0 ; i < USB_MAXINTERFACES ; i++) - if (dev_handle->claimed_interfaces & (1 << i)) + if (dev_handle->claimed_interfaces & (1U << i)) libusb_release_interface (dev_handle, i); if (0 == dpriv->open_count) { @@ -1168,22 +1174,24 @@ static enum libusb_error darwin_set_configuration(struct libusb_device_handle *d IOReturn kresult; int i; + assert(config >= 0 && config <= UINT8_MAX); + /* Setting configuration will invalidate the interface, so we need to reclaim it. First, dispose of existing interfaces, if any. */ for (i = 0 ; i < USB_MAXINTERFACES ; i++) - if (dev_handle->claimed_interfaces & (1 << i)) + if (dev_handle->claimed_interfaces & (1U << i)) darwin_release_interface (dev_handle, i); - kresult = (*(dpriv->device))->SetConfiguration (dpriv->device, config); + kresult = (*(dpriv->device))->SetConfiguration (dpriv->device, (UInt8)config); if (kresult != kIOReturnSuccess) return darwin_to_libusb (kresult); /* Reclaim any interfaces. */ for (i = 0 ; i < USB_MAXINTERFACES ; i++) - if (dev_handle->claimed_interfaces & (1 << i)) + if (dev_handle->claimed_interfaces & (1U << i)) darwin_claim_interface (dev_handle, i); - dpriv->active_config = config; + dpriv->active_config = (UInt8)config; return LIBUSB_SUCCESS; } @@ -1248,7 +1256,7 @@ static enum libusb_error get_endpoints (struct libusb_device_handle *dev_handle, } /* iterate through pipe references */ - for (int i = 1 ; i <= numep ; i++) { + for (UInt8 i = 1 ; i <= numep ; i++) { kresult = (*(cInterface->interface))->GetPipeProperties(cInterface->interface, i, &direction, &number, &dont_care1, &dont_care2, &dont_care3); @@ -1273,7 +1281,7 @@ static enum libusb_error get_endpoints (struct libusb_device_handle *dev_handle, cInterface->endpoint_addrs[i - 1] = endpoint_desc->bEndpointAddress; } else { - cInterface->endpoint_addrs[i - 1] = (((kUSBIn == direction) << kUSBRqDirnShift) | (number & LIBUSB_ENDPOINT_ADDRESS_MASK)); + cInterface->endpoint_addrs[i - 1] = (UInt8)(((kUSBIn == direction) << kUSBRqDirnShift) | (number & LIBUSB_ENDPOINT_ADDRESS_MASK)); } usbi_dbg ("interface: %i pipe %i: dir: %i number: %i", iface, i, cInterface->endpoint_addrs[i - 1] >> kUSBRqDirnShift, @@ -1294,10 +1302,12 @@ static int darwin_claim_interface(struct libusb_device_handle *dev_handle, int i IOCFPlugInInterface **plugInInterface = NULL; SInt32 score; + assert(iface >= 0 && iface <= UINT8_MAX); + /* current interface */ struct darwin_interface *cInterface = &priv->interfaces[iface]; - kresult = darwin_get_interface (dpriv->device, iface, &usbInterface); + kresult = darwin_get_interface (dpriv->device, (uint8_t)iface, &usbInterface); if (kresult != kIOReturnSuccess) return darwin_to_libusb (kresult); @@ -1312,7 +1322,7 @@ static int darwin_claim_interface(struct libusb_device_handle *dev_handle, int i return ret; } - kresult = darwin_get_interface (dpriv->device, iface, &usbInterface); + kresult = darwin_get_interface (dpriv->device, (uint8_t)iface, &usbInterface); if (kresult != kIOReturnSuccess) { usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult)); return darwin_to_libusb (kresult); @@ -1434,7 +1444,8 @@ static int darwin_set_interface_altsetting(struct libusb_device_handle *dev_hand if (!cInterface->interface) return LIBUSB_ERROR_NO_DEVICE; - kresult = (*(cInterface->interface))->SetAlternateInterface (cInterface->interface, altsetting); + assert(altsetting >= 0 && altsetting <= UINT8_MAX); + kresult = (*(cInterface->interface))->SetAlternateInterface (cInterface->interface, (UInt8)altsetting); if (kresult != kIOReturnSuccess) darwin_reset_device (dev_handle); @@ -1478,7 +1489,7 @@ static int darwin_reset_device(struct libusb_device_handle *dev_handle) { IOUSBConfigurationDescriptor configuration; bool reenumerate = false; IOReturn kresult; - int i; + UInt8 i; /* from macOS 10.11 ResetDevice no longer does anything so just use USBDeviceReEnumerate */ kresult = (*(dpriv->device))->USBDeviceReEnumerate (dpriv->device, 0); @@ -1530,7 +1541,8 @@ static int darwin_kernel_driver_active(struct libusb_device_handle *dev_handle, CFTypeRef driver; IOReturn kresult; - kresult = darwin_get_interface (dpriv->device, interface, &usbInterface); + assert(interface >= 0 && interface <= UINT8_MAX); + kresult = darwin_get_interface (dpriv->device, (uint8_t)interface, &usbInterface); if (kresult != kIOReturnSuccess) { usbi_err (HANDLE_CTX (dev_handle), "darwin_get_interface: %s", darwin_error_str(kresult)); @@ -1611,21 +1623,21 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) { if (transferType == kUSBInterrupt) { if (IS_XFERIN(transfer)) ret = (*(cInterface->interface))->ReadPipeAsync(cInterface->interface, pipeRef, transfer->buffer, - transfer->length, darwin_async_io_callback, itransfer); + (UInt32)transfer->length, darwin_async_io_callback, itransfer); else ret = (*(cInterface->interface))->WritePipeAsync(cInterface->interface, pipeRef, transfer->buffer, - transfer->length, darwin_async_io_callback, itransfer); + (UInt32)transfer->length, darwin_async_io_callback, itransfer); } else { itransfer->timeout_flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT; if (IS_XFERIN(transfer)) ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer, - transfer->length, transfer->timeout, transfer->timeout, - darwin_async_io_callback, (void *)itransfer); + (UInt32)transfer->length, transfer->timeout, transfer->timeout, + darwin_async_io_callback, itransfer); else ret = (*(cInterface->interface))->WritePipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer, - transfer->length, transfer->timeout, transfer->timeout, - darwin_async_io_callback, (void *)itransfer); + (UInt32)transfer->length, transfer->timeout, transfer->timeout, + darwin_async_io_callback, itransfer); } if (ret) @@ -1652,12 +1664,12 @@ static int submit_stream_transfer(struct usbi_transfer *itransfer) { if (IS_XFERIN(transfer)) ret = (*(cInterface->interface))->ReadStreamsPipeAsyncTO(cInterface->interface, pipeRef, itransfer->stream_id, - transfer->buffer, transfer->length, transfer->timeout, - transfer->timeout, darwin_async_io_callback, (void *)itransfer); + transfer->buffer, (UInt32)transfer->length, transfer->timeout, + transfer->timeout, darwin_async_io_callback, itransfer); else ret = (*(cInterface->interface))->WriteStreamsPipeAsyncTO(cInterface->interface, pipeRef, itransfer->stream_id, - transfer->buffer, transfer->length, transfer->timeout, - transfer->timeout, darwin_async_io_callback, (void *)itransfer); + transfer->buffer, (UInt32)transfer->length, transfer->timeout, + transfer->timeout, darwin_async_io_callback, itransfer); if (ret) usbi_err (TRANSFER_CTX (transfer), "bulk stream transfer failed (dir = %s): %s (code = 0x%08x)", IS_XFERIN(transfer) ? "In" : "Out", @@ -1688,14 +1700,17 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) { if (!tpriv->isoc_framelist) { tpriv->num_iso_packets = transfer->num_iso_packets; - tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc (transfer->num_iso_packets, sizeof(IOUSBIsocFrame)); + tpriv->isoc_framelist = (IOUSBIsocFrame*) calloc ((size_t)transfer->num_iso_packets, sizeof(IOUSBIsocFrame)); if (!tpriv->isoc_framelist) return LIBUSB_ERROR_NO_MEM; } /* copy the frame list from the libusb descriptor (the structures differ only is member order) */ - for (i = 0 ; i < transfer->num_iso_packets ; i++) - tpriv->isoc_framelist[i].frReqCount = transfer->iso_packet_desc[i].length; + for (i = 0 ; i < transfer->num_iso_packets ; i++) { + unsigned int length = transfer->iso_packet_desc[i].length; + assert(length <= UINT16_MAX); + tpriv->isoc_framelist[i].frReqCount = (UInt16)length; + } /* determine the interface/endpoint to use */ if (ep_to_pipeRef (transfer->dev_handle, transfer->endpoint, &pipeRef, NULL, &cInterface) != 0) { @@ -1730,19 +1745,19 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) { /* submit the request */ if (IS_XFERIN(transfer)) kresult = (*(cInterface->interface))->ReadIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame, - transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback, + (UInt32)transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback, itransfer); else kresult = (*(cInterface->interface))->WriteIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame, - transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback, + (UInt32)transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback, itransfer); if (LIBUSB_SPEED_FULL == transfer->dev_handle->dev->speed) /* Full speed */ - cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets * (1 << (interval - 1)); + cInterface->frames[transfer->endpoint] = frame + (UInt32)transfer->num_iso_packets * (1U << (interval - 1)); else /* High/super speed */ - cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets * (1 << (interval - 1)) / 8; + cInterface->frames[transfer->endpoint] = frame + (UInt32)transfer->num_iso_packets * (1U << (interval - 1)) / 8; if (kresult != kIOReturnSuccess) { usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", IS_XFERIN(transfer) ? "In" : "Out", @@ -2052,7 +2067,8 @@ static int darwin_alloc_streams (struct libusb_device_handle *dev_handle, uint32 return darwin_to_libusb(rc); } - return num_streams; + assert(num_streams <= INT_MAX); + return (int)num_streams; } static int darwin_free_streams (struct libusb_device_handle *dev_handle, unsigned char *endpoints, int num_endpoints) { diff --git a/libusb/os/darwin_usb.h b/libusb/os/darwin_usb.h index 474567f..8a728bc 100644 --- a/libusb/os/darwin_usb.h +++ b/libusb/os/darwin_usb.h @@ -155,7 +155,7 @@ struct darwin_cached_device { UInt32 location; UInt64 parent_session; UInt64 session; - UInt16 address; + USBDeviceAddress address; char sys_path[21]; usb_device_t **device; int open_count; diff --git a/libusb/os/haiku_usb_backend.cpp b/libusb/os/haiku_usb_backend.cpp index d3de8cc..01fba25 100644 --- a/libusb/os/haiku_usb_backend.cpp +++ b/libusb/os/haiku_usb_backend.cpp @@ -243,7 +243,7 @@ USBDeviceHandle::~USBDeviceHandle() if (fRawFD > 0) close(fRawFD); for(int i = 0; i < 32; i++) { - if (fClaimedInterfaces & (1 << i)) + if (fClaimedInterfaces & (1U << i)) ReleaseInterface(i); } delete_sem(fTransfersSem); @@ -256,7 +256,7 @@ USBDeviceHandle::ClaimInterface(int inumber) { int status = fUSBDevice->ClaimInterface(inumber); if (status == LIBUSB_SUCCESS) - fClaimedInterfaces |= (1 << inumber); + fClaimedInterfaces |= (1U << inumber); return status; } @@ -264,7 +264,7 @@ int USBDeviceHandle::ReleaseInterface(int inumber) { fUSBDevice->ReleaseInterface(inumber); - fClaimedInterfaces &= ~(1 << inumber); + fClaimedInterfaces &= ~(1U << inumber); return LIBUSB_SUCCESS; } @@ -388,15 +388,15 @@ int USBDevice::ClaimInterface(int interface) { if (interface > ActiveConfiguration()->number_interfaces) return LIBUSB_ERROR_NOT_FOUND; - if (fClaimedInterfaces & (1 << interface)) + if (fClaimedInterfaces & (1U << interface)) return LIBUSB_ERROR_BUSY; - fClaimedInterfaces |= (1 << interface); + fClaimedInterfaces |= (1U << interface); return LIBUSB_SUCCESS; } int USBDevice::ReleaseInterface(int interface) { - fClaimedInterfaces &= ~(1 << interface); + fClaimedInterfaces &= ~(1U << interface); return LIBUSB_SUCCESS; } diff --git a/libusb/version_nano.h b/libusb/version_nano.h index da4558f..27b0268 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11343 +#define LIBUSB_NANO 11344