Skip to content

Commit

Permalink
Update to latest libusb master
Browse files Browse the repository at this point in the history
Merge commit '67fcf8a2754c6fa604ad67ffa232903a89ddbef2'
  • Loading branch information
Youw committed Apr 7, 2024
2 parents 8782527 + 67fcf8a commit c07bf58
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 66 deletions.
47 changes: 47 additions & 0 deletions libusb/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
Checks: "-*,\
boost-*,\
bugprone-*,\
-bugprone-assignment-in-if-condition,\
-bugprone-branch-clone,\
-bugprone-easily-swappable-parameters,\
-bugprone-implicit-widening-of-multiplication-result,\
-bugprone-macro-parentheses,\
-bugprone-misplaced-widening-cast,\
-bugprone-narrowing-conversions,\
-bugprone-reserved-identifier,\
-bugprone-signed-char-misuse,\
-bugprone-suspicious-string-compare,\
-bugprone-switch-missing-default-case,\
-bugprone-unsafe-functions,\
-bugprone-too-small-loop-variable,\
clang-analyzer-*,\
-clang-analyzer-core.NullDereference,\
-clang-analyzer-deadcode.DeadStores,\
-clang-analyzer-optin.portability.UnixAPI,\
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,\
-clang-analyzer-security.insecureAPI.strcpy,\
-clang-analyzer-unix.Malloc,\
misc-*,\
-misc-no-recursion,\
-misc-include-cleaner,\
modernize-*,\
-modernize-macro-to-enum,\
performance-*,\
-performance-no-int-to-ptr,\
-performance-type-promotion-in-math-fn,\
portability-*,\
readability-*,\
-readability-braces-around-statements,\
-readability-else-after-return,\
-readability-identifier-length,\
-readability-function-cognitive-complexity,\
-readability-inconsistent-declaration-parameter-name,\
-readability-isolate-declaration,\
-readability-magic-numbers,\
-readability-non-const-parameter,\
-readability-uppercase-literal-suffix,\
-readability-misleading-indentation,\
"
#WarningsAsErrors: "*"
...
20 changes: 20 additions & 0 deletions libusb/Xcode/common.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ CLANG_WARN_INT_CONVERSION = YES
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES
CLANG_WARN_STRICT_PROTOTYPES = YES
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES
CLANG_WARN_COMPLETION_HANDLER_MISUSE = YES
CLANG_WARN_IMPLICIT_FALLTHROUGH = YES
CLANG_WARN_FRAMEWORK_INCLUDE_PRIVATE_FROM_PUBLIC = YES
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES
CLANG_WARN_SEMICOLON_BEFORE_METHOD_BODY = YES
GCC_WARN_SIGN_COMPARE = YES
CLANG_WARN__EXIT_TIME_DESTRUCTORS = YES
GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES
GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES
CLANG_WARN_ATOMIC_IMPLICIT_SEQ_CST = YES
CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES
CLANG_WARN_OBJC_INTERFACE_IVARS = YES
GCC_WARN_STRICT_SELECTOR_MATCH = YES
CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK = YES
CLANG_WARN_UNREACHABLE_CODE = YES
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES
CLANG_WARN_SUSPICIOUS_MOVE = YES
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES
GCC_WARN_UNDECLARED_SELECTOR = YES

// Static analyzer warnings.
CLANG_ANALYZER_NONNULL = YES
Expand Down
35 changes: 20 additions & 15 deletions libusb/examples/xusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in,
double device_size;
uint8_t cdb[16]; // SCSI Command Descriptor Block
uint8_t buffer[64];
char vid[9], pid[9], rev[5];
unsigned char vid[9], pid[9], rev[5];
unsigned char *data;
FILE *fd;

Expand Down Expand Up @@ -560,7 +560,7 @@ static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int typ
uint8_t i, j = 0;
uint8_t offset;
int record_size[3] = {0, 0, 0};
int nb_bits = 0, nb_items = 0;
unsigned int nb_bits = 0, nb_items = 0;
bool found_record_marker;

found_record_marker = false;
Expand All @@ -575,7 +575,7 @@ static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int typ
case 0x94: // count
nb_items = 0;
for (j=1; j<offset; j++) {
nb_items = ((uint32_t)hid_report_descriptor[i+j]) << (8*(j-1));
nb_items = ((unsigned int)hid_report_descriptor[i+j]) << (8U*(j-1U));
}
break;
case 0x80: // input
Expand Down Expand Up @@ -623,9 +623,9 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
printf(" Failed\n");
return -1;
}
display_buffer_hex(hid_report_descriptor, descriptor_size);
display_buffer_hex(hid_report_descriptor, (unsigned int)descriptor_size);
if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != (size_t)descriptor_size) {
if (fwrite(hid_report_descriptor, 1, (size_t)descriptor_size, fd) != (size_t)descriptor_size) {
printf(" Error writing descriptor to file\n");
}
fclose(fd);
Expand All @@ -634,8 +634,10 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_FEATURE);
if (size <= 0) {
printf("\nSkipping Feature Report readout (None detected)\n");
} else if (size > UINT16_MAX) {
printf("\nSkipping Feature Report readout (bigger than UINT16_MAX)\n");
} else {
report_buffer = (uint8_t*) calloc(size, 1);
report_buffer = (uint8_t*) calloc(1, (size_t)size);
if (report_buffer == NULL) {
return -1;
}
Expand All @@ -644,7 +646,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 0, report_buffer, (uint16_t)size, 5000);
if (r >= 0) {
display_buffer_hex(report_buffer, size);
display_buffer_hex(report_buffer, (unsigned int)size);
} else {
switch(r) {
case LIBUSB_ERROR_NOT_FOUND:
Expand All @@ -665,8 +667,10 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_INPUT);
if (size <= 0) {
printf("\nSkipping Input Report readout (None detected)\n");
} else if (size > UINT16_MAX) {
printf("\nSkipping Input Report readout (bigger than UINT16_MAX)\n");
} else {
report_buffer = (uint8_t*) calloc(size, 1);
report_buffer = (uint8_t*) calloc(1, (size_t)size);
if (report_buffer == NULL) {
return -1;
}
Expand All @@ -675,7 +679,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, report_buffer, (uint16_t)size, 5000);
if (r >= 0) {
display_buffer_hex(report_buffer, size);
display_buffer_hex(report_buffer, (unsigned int)size);
} else {
switch(r) {
case LIBUSB_ERROR_TIMEOUT:
Expand All @@ -695,7 +699,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
printf("\nTesting interrupt read using endpoint %02X...\n", endpoint_in);
r = libusb_interrupt_transfer(handle, endpoint_in, report_buffer, size, &size, 5000);
if (r >= 0) {
display_buffer_hex(report_buffer, size);
display_buffer_hex(report_buffer, (unsigned int)size);
} else {
printf(" %s\n", libusb_strerror((enum libusb_error)r));
}
Expand Down Expand Up @@ -753,7 +757,7 @@ static void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uin
perr(" Failed: %s", libusb_strerror((enum libusb_error)r));
return;
} else {
display_buffer_hex(os_desc, r);
display_buffer_hex(os_desc, (unsigned int)r);
}
}
}
Expand Down Expand Up @@ -824,7 +828,7 @@ static int test_device(uint16_t vid, uint16_t pid)
struct libusb_device_descriptor dev_desc;
const char* const speed_name[6] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
"480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)", "10000 Mbit/s (USB SuperSpeedPlus)" };
char string[128];
unsigned char string[128];
uint8_t string_index[3]; // indexes of the string descriptors
uint8_t endpoint_in = 0, endpoint_out = 0; // default IN and OUT endpoints

Expand Down Expand Up @@ -960,13 +964,13 @@ static int test_device(uint16_t vid, uint16_t pid)
if (string_index[i] == 0) {
continue;
}
if (libusb_get_string_descriptor_ascii(handle, string_index[i], (unsigned char*)string, sizeof(string)) > 0) {
if (libusb_get_string_descriptor_ascii(handle, string_index[i], string, sizeof(string)) > 0) {
printf(" String (0x%02X): \"%s\"\n", string_index[i], string);
}
}

printf("\nReading OS string descriptor:");
r = libusb_get_string_descriptor(handle, MS_OS_DESC_STRING_INDEX, 0, (unsigned char*)string, MS_OS_DESC_STRING_LENGTH);
r = libusb_get_string_descriptor(handle, MS_OS_DESC_STRING_INDEX, 0, string, MS_OS_DESC_STRING_LENGTH);
if (r == MS_OS_DESC_STRING_LENGTH && memcmp(ms_os_desc_string, string, sizeof(ms_os_desc_string)) == 0) {
// If this is a Microsoft OS String Descriptor,
// attempt to read the WinUSB extended Feature Descriptors
Expand All @@ -991,7 +995,7 @@ static int test_device(uint16_t vid, uint16_t pid)
printf(" bFunctionSubClass: %02X\n", iad->bFunctionSubClass);
printf(" bFunctionProtocol: %02X\n", iad->bFunctionProtocol);
if (iad->iFunction) {
if (libusb_get_string_descriptor_ascii(handle, iad->iFunction, (unsigned char*)string, sizeof(string)) > 0)
if (libusb_get_string_descriptor_ascii(handle, iad->iFunction, string, sizeof(string)) > 0)
printf(" iFunction: %u (%s)\n", iad->iFunction, string);
else
printf(" iFunction: %u (libusb_get_string_descriptor_ascii failed!)\n", iad->iFunction);
Expand All @@ -1017,6 +1021,7 @@ static int test_device(uint16_t vid, uint16_t pid)
break;
case USE_SCSI:
CALL_CHECK_CLOSE(test_mass_storage(handle, endpoint_in, endpoint_out), handle);
break;
case USE_GENERIC:
break;
}
Expand Down
6 changes: 3 additions & 3 deletions libusb/libusb/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
dev = dev->parent_dev;
}
if (i < port_numbers_len)
memmove(port_numbers, &port_numbers[i], port_numbers_len - i);
memmove(port_numbers, &port_numbers[i], (size_t)(port_numbers_len - i));
return port_numbers_len - i;
}

Expand Down Expand Up @@ -1014,7 +1014,7 @@ uint8_t API_EXPORTED libusb_get_device_address(libusb_device *dev)
*/
int API_EXPORTED libusb_get_device_speed(libusb_device *dev)
{
return dev->speed;
return (int)(dev->speed);
}

static const struct libusb_endpoint_descriptor *find_endpoint(
Expand Down Expand Up @@ -2456,7 +2456,7 @@ int API_EXPORTED libusb_init_context(libusb_context **ctx, const struct libusb_i
_ctx->debug = get_env_debug_level();
_ctx->debug_fixed = 1;
} else if (default_context_options[LIBUSB_OPTION_LOG_LEVEL].is_set) {
_ctx->debug = default_context_options[LIBUSB_OPTION_LOG_LEVEL].arg.ival;
_ctx->debug = (enum libusb_log_level)default_context_options[LIBUSB_OPTION_LOG_LEVEL].arg.ival;
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions libusb/libusb/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static int parse_endpoint(struct libusb_context *ctx,
if (!extra)
return LIBUSB_ERROR_NO_MEM;

memcpy(extra, begin, len);
memcpy(extra, begin, (size_t)len);
endpoint->extra = extra;
endpoint->extra_length = len;

Expand Down Expand Up @@ -286,7 +286,7 @@ static int parse_interface(libusb_context *ctx,
goto err;
}

memcpy(extra, begin, len);
memcpy(extra, begin, (size_t)len);
ifp->extra = extra;
ifp->extra_length = len;
}
Expand Down Expand Up @@ -431,7 +431,7 @@ static int parse_configuration(struct libusb_context *ctx,
goto err;
}

memcpy(extra + config->extra_length, begin, len);
memcpy(extra + config->extra_length, begin, (size_t)len);
config->extra = extra;
config->extra_length += len;
}
Expand Down Expand Up @@ -1241,7 +1241,7 @@ static int parse_iad_array(struct libusb_context *ctx,

iad_array->iad = NULL;
if (iad_array->length > 0) {
iad = calloc(iad_array->length, sizeof(*iad));
iad = calloc((size_t)iad_array->length, sizeof(*iad));
if (!iad)
return LIBUSB_ERROR_NO_MEM;

Expand Down
35 changes: 27 additions & 8 deletions libusb/libusb/hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ void usbi_hotplug_init(struct libusb_context *ctx)
usbi_atomic_store(&ctx->hotplug_ready, 1);
}

static void usbi_recursively_remove_parents(struct libusb_device *dev, struct libusb_device *next_dev)
{
if (dev && dev->parent_dev) {
if (usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
/* The parent was processed before this device in the list and
* therefore has its ref count already decremented for its own ref.
* The only remaining counted ref comes from its remaining single child.
* It will thus be released when its child will be released. So we
* can remove it from the list. This is safe as parent_dev cannot be
* equal to next_dev given that we know at this point that it was
* previously seen in the list. */
assert (dev->parent_dev != next_dev);
if (dev->parent_dev->list.next && dev->parent_dev->list.prev) {
list_del(&dev->parent_dev->list);
}
}

usbi_recursively_remove_parents(dev->parent_dev, next_dev);
}
}

void usbi_hotplug_exit(struct libusb_context *ctx)
{
struct usbi_hotplug_callback *hotplug_cb, *next_cb;
Expand Down Expand Up @@ -193,23 +214,21 @@ void usbi_hotplug_exit(struct libusb_context *ctx)
free(msg);
}

/* free all discovered devices. due to parent references loop until no devices are freed. */
usbi_mutex_lock(&ctx->usb_devs_lock); /* hotplug thread might still be processing an already triggered event, possibly accessing this list as well */
/* free all discovered devices */
for_each_device_safe(ctx, dev, next_dev) {
/* remove the device from the usb_devs list only if there are no
* references held, otherwise leave it on the list so that a
* warning message will be shown */
if (usbi_atomic_load(&dev->refcnt) == 1) {
list_del(&dev->list);
}
if (dev->parent_dev && usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
/* the parent was before this device in the list and will be released.
remove it from the list. this is safe as parent_dev can not be
equal to next_dev. */
assert (dev->parent_dev != next_dev);
list_del(&dev->parent_dev->list);
}

usbi_recursively_remove_parents(dev, next_dev);

libusb_unref_device(dev);
}
usbi_mutex_unlock(&ctx->usb_devs_lock);

usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
}
Expand Down
1 change: 1 addition & 0 deletions libusb/libusb/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,7 @@ int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
flags = transfer->flags;
transfer->status = status;
transfer->actual_length = itransfer->transferred;
assert(transfer->actual_length >= 0);
usbi_dbg(ctx, "transfer %p has callback %p",
(void *) transfer, transfer->callback);
if (transfer->callback) {
Expand Down
Loading

0 comments on commit c07bf58

Please sign in to comment.