hosted/bmp_libusb: Rewrote the string reader logic to not violate the USB spec and properly handle libusb errors

This commit is contained in:
dragonmux 2022-04-01 12:46:13 -04:00 committed by Rachel Mant
parent c7eba0a439
commit 94e9281404
1 changed files with 50 additions and 15 deletions

View File

@ -183,24 +183,59 @@ rescan:
} }
continue; continue;
} }
res = libusb_get_string_descriptor_ascii( /* If the device even has a serial number string, fetch it */
handle, desc.iSerialNumber, (uint8_t*)serial, if (desc.iSerialNumber) {
sizeof(serial)); res = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
if (cl_opts->opt_serial && (res <= 0 || (uint8_t *)serial, sizeof(serial));
!strstr(serial, cl_opts->opt_serial))) { /* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (res < 0 && res != LIBUSB_ERROR_PIPE) {
libusb_close(handle);
continue;
}
/* Device has no serial and that's ok. */
else if (res <= 0)
serial[0] = '\0';
}
else
serial[0] = '\0';
if (cl_opts->opt_serial && !strstr(serial, cl_opts->opt_serial)) {
libusb_close(handle); libusb_close(handle);
continue; continue;
} }
if (res < 0) /* Attempt to get the manufacturer string */
serial[0] = 0; if (desc.iManufacturer) {
manufacturer[0] = 0; res = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
res = libusb_get_string_descriptor_ascii( (uint8_t *)manufacturer, sizeof(manufacturer));
handle, desc.iManufacturer, (uint8_t *)manufacturer, /* If the call fails and it's not because the device gave us STALL, continue to the next one */
sizeof(manufacturer)); if (res < 0 && res != LIBUSB_ERROR_PIPE) {
product[0] = 0; DEBUG_WARN("WARN: libusb_get_string_descriptor_ascii() call to fetch manufacturer string failed: %s\n",
res = libusb_get_string_descriptor_ascii( libusb_strerror(res));
handle, desc.iProduct, (uint8_t *)product, libusb_close(handle);
sizeof(product)); continue;
}
/* Device has no manufacturer string and that's ok. */
else if (res <= 0)
manufacturer[0] = '\0';
}
else
manufacturer[0] = '\0';
/* Attempt to get the product string */
if (desc.iProduct) {
res = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
(uint8_t *)product, sizeof(product));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (res < 0 && res != LIBUSB_ERROR_PIPE) {
DEBUG_WARN("WARN: libusb_get_string_descriptor_ascii() call to fetch product string failed: %s\n",
libusb_strerror(res));
libusb_close(handle);
continue;
}
/* Device has no product string and that's ok. */
else if (res <= 0)
product[0] = '\0';
}
else
product[0] = '\0';
libusb_close(handle); libusb_close(handle);
if (cl_opts->opt_ident_string) { if (cl_opts->opt_ident_string) {
char *match_manu = NULL; char *match_manu = NULL;