Skip to content

Commit c07bf58

Browse files
committed
Update to latest libusb master
Merge commit '67fcf8a2754c6fa604ad67ffa232903a89ddbef2'
2 parents 8782527 + 67fcf8a commit c07bf58

File tree

12 files changed

+168
-66
lines changed

12 files changed

+168
-66
lines changed

libusb/.clang-tidy

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
Checks: "-*,\
3+
boost-*,\
4+
bugprone-*,\
5+
-bugprone-assignment-in-if-condition,\
6+
-bugprone-branch-clone,\
7+
-bugprone-easily-swappable-parameters,\
8+
-bugprone-implicit-widening-of-multiplication-result,\
9+
-bugprone-macro-parentheses,\
10+
-bugprone-misplaced-widening-cast,\
11+
-bugprone-narrowing-conversions,\
12+
-bugprone-reserved-identifier,\
13+
-bugprone-signed-char-misuse,\
14+
-bugprone-suspicious-string-compare,\
15+
-bugprone-switch-missing-default-case,\
16+
-bugprone-unsafe-functions,\
17+
-bugprone-too-small-loop-variable,\
18+
clang-analyzer-*,\
19+
-clang-analyzer-core.NullDereference,\
20+
-clang-analyzer-deadcode.DeadStores,\
21+
-clang-analyzer-optin.portability.UnixAPI,\
22+
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,\
23+
-clang-analyzer-security.insecureAPI.strcpy,\
24+
-clang-analyzer-unix.Malloc,\
25+
misc-*,\
26+
-misc-no-recursion,\
27+
-misc-include-cleaner,\
28+
modernize-*,\
29+
-modernize-macro-to-enum,\
30+
performance-*,\
31+
-performance-no-int-to-ptr,\
32+
-performance-type-promotion-in-math-fn,\
33+
portability-*,\
34+
readability-*,\
35+
-readability-braces-around-statements,\
36+
-readability-else-after-return,\
37+
-readability-identifier-length,\
38+
-readability-function-cognitive-complexity,\
39+
-readability-inconsistent-declaration-parameter-name,\
40+
-readability-isolate-declaration,\
41+
-readability-magic-numbers,\
42+
-readability-non-const-parameter,\
43+
-readability-uppercase-literal-suffix,\
44+
-readability-misleading-indentation,\
45+
"
46+
#WarningsAsErrors: "*"
47+
...

libusb/Xcode/common.xcconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ CLANG_WARN_INT_CONVERSION = YES
6565
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES
6666
CLANG_WARN_STRICT_PROTOTYPES = YES
6767
CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES
68+
CLANG_WARN_COMPLETION_HANDLER_MISUSE = YES
69+
CLANG_WARN_IMPLICIT_FALLTHROUGH = YES
70+
CLANG_WARN_FRAMEWORK_INCLUDE_PRIVATE_FROM_PUBLIC = YES
71+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES
72+
CLANG_WARN_SEMICOLON_BEFORE_METHOD_BODY = YES
73+
GCC_WARN_SIGN_COMPARE = YES
74+
CLANG_WARN__EXIT_TIME_DESTRUCTORS = YES
75+
GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES
76+
GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES
77+
CLANG_WARN_ATOMIC_IMPLICIT_SEQ_CST = YES
78+
CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES
79+
CLANG_WARN_OBJC_INTERFACE_IVARS = YES
80+
GCC_WARN_STRICT_SELECTOR_MATCH = YES
81+
CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK = YES
82+
CLANG_WARN_UNREACHABLE_CODE = YES
83+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES
84+
CLANG_WARN_SUSPICIOUS_MOVE = YES
85+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES
86+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES
87+
GCC_WARN_UNDECLARED_SELECTOR = YES
6888

6989
// Static analyzer warnings.
7090
CLANG_ANALYZER_NONNULL = YES

libusb/examples/xusb.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static int test_mass_storage(libusb_device_handle *handle, uint8_t endpoint_in,
461461
double device_size;
462462
uint8_t cdb[16]; // SCSI Command Descriptor Block
463463
uint8_t buffer[64];
464-
char vid[9], pid[9], rev[5];
464+
unsigned char vid[9], pid[9], rev[5];
465465
unsigned char *data;
466466
FILE *fd;
467467

@@ -560,7 +560,7 @@ static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int typ
560560
uint8_t i, j = 0;
561561
uint8_t offset;
562562
int record_size[3] = {0, 0, 0};
563-
int nb_bits = 0, nb_items = 0;
563+
unsigned int nb_bits = 0, nb_items = 0;
564564
bool found_record_marker;
565565

566566
found_record_marker = false;
@@ -575,7 +575,7 @@ static int get_hid_record_size(uint8_t *hid_report_descriptor, int size, int typ
575575
case 0x94: // count
576576
nb_items = 0;
577577
for (j=1; j<offset; j++) {
578-
nb_items = ((uint32_t)hid_report_descriptor[i+j]) << (8*(j-1));
578+
nb_items = ((unsigned int)hid_report_descriptor[i+j]) << (8U*(j-1U));
579579
}
580580
break;
581581
case 0x80: // input
@@ -623,9 +623,9 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
623623
printf(" Failed\n");
624624
return -1;
625625
}
626-
display_buffer_hex(hid_report_descriptor, descriptor_size);
626+
display_buffer_hex(hid_report_descriptor, (unsigned int)descriptor_size);
627627
if ((binary_dump) && ((fd = fopen(binary_name, "w")) != NULL)) {
628-
if (fwrite(hid_report_descriptor, 1, descriptor_size, fd) != (size_t)descriptor_size) {
628+
if (fwrite(hid_report_descriptor, 1, (size_t)descriptor_size, fd) != (size_t)descriptor_size) {
629629
printf(" Error writing descriptor to file\n");
630630
}
631631
fclose(fd);
@@ -634,8 +634,10 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
634634
size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_FEATURE);
635635
if (size <= 0) {
636636
printf("\nSkipping Feature Report readout (None detected)\n");
637+
} else if (size > UINT16_MAX) {
638+
printf("\nSkipping Feature Report readout (bigger than UINT16_MAX)\n");
637639
} else {
638-
report_buffer = (uint8_t*) calloc(size, 1);
640+
report_buffer = (uint8_t*) calloc(1, (size_t)size);
639641
if (report_buffer == NULL) {
640642
return -1;
641643
}
@@ -644,7 +646,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
644646
r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
645647
HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE<<8)|0, 0, report_buffer, (uint16_t)size, 5000);
646648
if (r >= 0) {
647-
display_buffer_hex(report_buffer, size);
649+
display_buffer_hex(report_buffer, (unsigned int)size);
648650
} else {
649651
switch(r) {
650652
case LIBUSB_ERROR_NOT_FOUND:
@@ -665,8 +667,10 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
665667
size = get_hid_record_size(hid_report_descriptor, descriptor_size, HID_REPORT_TYPE_INPUT);
666668
if (size <= 0) {
667669
printf("\nSkipping Input Report readout (None detected)\n");
670+
} else if (size > UINT16_MAX) {
671+
printf("\nSkipping Input Report readout (bigger than UINT16_MAX)\n");
668672
} else {
669-
report_buffer = (uint8_t*) calloc(size, 1);
673+
report_buffer = (uint8_t*) calloc(1, (size_t)size);
670674
if (report_buffer == NULL) {
671675
return -1;
672676
}
@@ -675,7 +679,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
675679
r = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE,
676680
HID_GET_REPORT, (HID_REPORT_TYPE_INPUT<<8)|0x00, 0, report_buffer, (uint16_t)size, 5000);
677681
if (r >= 0) {
678-
display_buffer_hex(report_buffer, size);
682+
display_buffer_hex(report_buffer, (unsigned int)size);
679683
} else {
680684
switch(r) {
681685
case LIBUSB_ERROR_TIMEOUT:
@@ -695,7 +699,7 @@ static int test_hid(libusb_device_handle *handle, uint8_t endpoint_in)
695699
printf("\nTesting interrupt read using endpoint %02X...\n", endpoint_in);
696700
r = libusb_interrupt_transfer(handle, endpoint_in, report_buffer, size, &size, 5000);
697701
if (r >= 0) {
698-
display_buffer_hex(report_buffer, size);
702+
display_buffer_hex(report_buffer, (unsigned int)size);
699703
} else {
700704
printf(" %s\n", libusb_strerror((enum libusb_error)r));
701705
}
@@ -753,7 +757,7 @@ static void read_ms_winsub_feature_descriptors(libusb_device_handle *handle, uin
753757
perr(" Failed: %s", libusb_strerror((enum libusb_error)r));
754758
return;
755759
} else {
756-
display_buffer_hex(os_desc, r);
760+
display_buffer_hex(os_desc, (unsigned int)r);
757761
}
758762
}
759763
}
@@ -824,7 +828,7 @@ static int test_device(uint16_t vid, uint16_t pid)
824828
struct libusb_device_descriptor dev_desc;
825829
const char* const speed_name[6] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)",
826830
"480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)", "10000 Mbit/s (USB SuperSpeedPlus)" };
827-
char string[128];
831+
unsigned char string[128];
828832
uint8_t string_index[3]; // indexes of the string descriptors
829833
uint8_t endpoint_in = 0, endpoint_out = 0; // default IN and OUT endpoints
830834

@@ -960,13 +964,13 @@ static int test_device(uint16_t vid, uint16_t pid)
960964
if (string_index[i] == 0) {
961965
continue;
962966
}
963-
if (libusb_get_string_descriptor_ascii(handle, string_index[i], (unsigned char*)string, sizeof(string)) > 0) {
967+
if (libusb_get_string_descriptor_ascii(handle, string_index[i], string, sizeof(string)) > 0) {
964968
printf(" String (0x%02X): \"%s\"\n", string_index[i], string);
965969
}
966970
}
967971

968972
printf("\nReading OS string descriptor:");
969-
r = libusb_get_string_descriptor(handle, MS_OS_DESC_STRING_INDEX, 0, (unsigned char*)string, MS_OS_DESC_STRING_LENGTH);
973+
r = libusb_get_string_descriptor(handle, MS_OS_DESC_STRING_INDEX, 0, string, MS_OS_DESC_STRING_LENGTH);
970974
if (r == MS_OS_DESC_STRING_LENGTH && memcmp(ms_os_desc_string, string, sizeof(ms_os_desc_string)) == 0) {
971975
// If this is a Microsoft OS String Descriptor,
972976
// attempt to read the WinUSB extended Feature Descriptors
@@ -991,7 +995,7 @@ static int test_device(uint16_t vid, uint16_t pid)
991995
printf(" bFunctionSubClass: %02X\n", iad->bFunctionSubClass);
992996
printf(" bFunctionProtocol: %02X\n", iad->bFunctionProtocol);
993997
if (iad->iFunction) {
994-
if (libusb_get_string_descriptor_ascii(handle, iad->iFunction, (unsigned char*)string, sizeof(string)) > 0)
998+
if (libusb_get_string_descriptor_ascii(handle, iad->iFunction, string, sizeof(string)) > 0)
995999
printf(" iFunction: %u (%s)\n", iad->iFunction, string);
9961000
else
9971001
printf(" iFunction: %u (libusb_get_string_descriptor_ascii failed!)\n", iad->iFunction);
@@ -1017,6 +1021,7 @@ static int test_device(uint16_t vid, uint16_t pid)
10171021
break;
10181022
case USE_SCSI:
10191023
CALL_CHECK_CLOSE(test_mass_storage(handle, endpoint_in, endpoint_out), handle);
1024+
break;
10201025
case USE_GENERIC:
10211026
break;
10221027
}

libusb/libusb/core.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
964964
dev = dev->parent_dev;
965965
}
966966
if (i < port_numbers_len)
967-
memmove(port_numbers, &port_numbers[i], port_numbers_len - i);
967+
memmove(port_numbers, &port_numbers[i], (size_t)(port_numbers_len - i));
968968
return port_numbers_len - i;
969969
}
970970

@@ -1014,7 +1014,7 @@ uint8_t API_EXPORTED libusb_get_device_address(libusb_device *dev)
10141014
*/
10151015
int API_EXPORTED libusb_get_device_speed(libusb_device *dev)
10161016
{
1017-
return dev->speed;
1017+
return (int)(dev->speed);
10181018
}
10191019

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

libusb/libusb/descriptor.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static int parse_endpoint(struct libusb_context *ctx,
156156
if (!extra)
157157
return LIBUSB_ERROR_NO_MEM;
158158

159-
memcpy(extra, begin, len);
159+
memcpy(extra, begin, (size_t)len);
160160
endpoint->extra = extra;
161161
endpoint->extra_length = len;
162162

@@ -286,7 +286,7 @@ static int parse_interface(libusb_context *ctx,
286286
goto err;
287287
}
288288

289-
memcpy(extra, begin, len);
289+
memcpy(extra, begin, (size_t)len);
290290
ifp->extra = extra;
291291
ifp->extra_length = len;
292292
}
@@ -431,7 +431,7 @@ static int parse_configuration(struct libusb_context *ctx,
431431
goto err;
432432
}
433433

434-
memcpy(extra + config->extra_length, begin, len);
434+
memcpy(extra + config->extra_length, begin, (size_t)len);
435435
config->extra = extra;
436436
config->extra_length += len;
437437
}
@@ -1241,7 +1241,7 @@ static int parse_iad_array(struct libusb_context *ctx,
12411241

12421242
iad_array->iad = NULL;
12431243
if (iad_array->length > 0) {
1244-
iad = calloc(iad_array->length, sizeof(*iad));
1244+
iad = calloc((size_t)iad_array->length, sizeof(*iad));
12451245
if (!iad)
12461246
return LIBUSB_ERROR_NO_MEM;
12471247

libusb/libusb/hotplug.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ void usbi_hotplug_init(struct libusb_context *ctx)
161161
usbi_atomic_store(&ctx->hotplug_ready, 1);
162162
}
163163

164+
static void usbi_recursively_remove_parents(struct libusb_device *dev, struct libusb_device *next_dev)
165+
{
166+
if (dev && dev->parent_dev) {
167+
if (usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
168+
/* The parent was processed before this device in the list and
169+
* therefore has its ref count already decremented for its own ref.
170+
* The only remaining counted ref comes from its remaining single child.
171+
* It will thus be released when its child will be released. So we
172+
* can remove it from the list. This is safe as parent_dev cannot be
173+
* equal to next_dev given that we know at this point that it was
174+
* previously seen in the list. */
175+
assert (dev->parent_dev != next_dev);
176+
if (dev->parent_dev->list.next && dev->parent_dev->list.prev) {
177+
list_del(&dev->parent_dev->list);
178+
}
179+
}
180+
181+
usbi_recursively_remove_parents(dev->parent_dev, next_dev);
182+
}
183+
}
184+
164185
void usbi_hotplug_exit(struct libusb_context *ctx)
165186
{
166187
struct usbi_hotplug_callback *hotplug_cb, *next_cb;
@@ -193,23 +214,21 @@ void usbi_hotplug_exit(struct libusb_context *ctx)
193214
free(msg);
194215
}
195216

196-
/* free all discovered devices. due to parent references loop until no devices are freed. */
217+
usbi_mutex_lock(&ctx->usb_devs_lock); /* hotplug thread might still be processing an already triggered event, possibly accessing this list as well */
218+
/* free all discovered devices */
197219
for_each_device_safe(ctx, dev, next_dev) {
198220
/* remove the device from the usb_devs list only if there are no
199221
* references held, otherwise leave it on the list so that a
200222
* warning message will be shown */
201223
if (usbi_atomic_load(&dev->refcnt) == 1) {
202224
list_del(&dev->list);
203225
}
204-
if (dev->parent_dev && usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
205-
/* the parent was before this device in the list and will be released.
206-
remove it from the list. this is safe as parent_dev can not be
207-
equal to next_dev. */
208-
assert (dev->parent_dev != next_dev);
209-
list_del(&dev->parent_dev->list);
210-
}
226+
227+
usbi_recursively_remove_parents(dev, next_dev);
228+
211229
libusb_unref_device(dev);
212230
}
231+
usbi_mutex_unlock(&ctx->usb_devs_lock);
213232

214233
usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
215234
}

libusb/libusb/io.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@ int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
17141714
flags = transfer->flags;
17151715
transfer->status = status;
17161716
transfer->actual_length = itransfer->transferred;
1717+
assert(transfer->actual_length >= 0);
17171718
usbi_dbg(ctx, "transfer %p has callback %p",
17181719
(void *) transfer, transfer->callback);
17191720
if (transfer->callback) {

0 commit comments

Comments
 (0)