Commit 5e649983e74a7e75f13b3d129cdbfe61e3f013c0

Sam Lantinga 2020-01-26T12:40:32

Don't enumerate devices we can't open

diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c
index bcf51ea..90ca815 100644
--- a/src/hidapi/libusb/hid.c
+++ b/src/hidapi/libusb/hid.c
@@ -608,25 +608,25 @@ struct hid_device_info  HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, 
 						/* Check the VID/PID against the arguments */
 						if ((vendor_id == 0x0 || vendor_id == dev_vid) &&
 						    (product_id == 0x0 || product_id == dev_pid)) {
-							struct hid_device_info *tmp;
+							res = libusb_open(dev, &handle);
 
-							/* VID/PID match. Create the record. */
-							tmp = (struct hid_device_info*) calloc(1, sizeof(struct hid_device_info));
-							if (cur_dev) {
-								cur_dev->next = tmp;
-							}
-							else {
-								root = tmp;
-							}
-							cur_dev = tmp;
+							if (res >= 0) {
+								struct hid_device_info *tmp;
 
-							/* Fill out the record */
-							cur_dev->next = NULL;
-							cur_dev->path = make_path(dev, interface_num);
+								/* VID/PID match. Create the record. */
+								tmp = (struct hid_device_info*) calloc(1, sizeof(struct hid_device_info));
+								if (cur_dev) {
+									cur_dev->next = tmp;
+								}
+								else {
+									root = tmp;
+								}
+								cur_dev = tmp;
 
-							res = libusb_open(dev, &handle);
+								/* Fill out the record */
+								cur_dev->next = NULL;
+								cur_dev->path = make_path(dev, interface_num);
 
-							if (res >= 0) {
 								/* Serial Number */
 								if (desc.iSerialNumber > 0)
 									cur_dev->serial_number =
@@ -704,19 +704,22 @@ struct hid_device_info  HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, 
 #endif /* INVASIVE_GET_USAGE */
 
 								libusb_close(handle);
-							}
-							/* VID/PID */
-							cur_dev->vendor_id = dev_vid;
-							cur_dev->product_id = dev_pid;
-
-							/* Release Number */
-							cur_dev->release_number = desc.bcdDevice;
-
-							/* Interface Number */
-							cur_dev->interface_number = interface_num;
-							cur_dev->interface_class = intf_desc->bInterfaceClass;
-							cur_dev->interface_subclass = intf_desc->bInterfaceSubClass;
-							cur_dev->interface_protocol = intf_desc->bInterfaceProtocol;
+
+								/* VID/PID */
+								cur_dev->vendor_id = dev_vid;
+								cur_dev->product_id = dev_pid;
+
+								/* Release Number */
+								cur_dev->release_number = desc.bcdDevice;
+
+								/* Interface Number */
+								cur_dev->interface_number = interface_num;
+								cur_dev->interface_class = intf_desc->bInterfaceClass;
+								cur_dev->interface_subclass = intf_desc->bInterfaceSubClass;
+								cur_dev->interface_protocol = intf_desc->bInterfaceProtocol;
+
+							} else
+								LOG("Can't open device 0x%.4x/0x%.4x during enumeration: %d\n", dev_vid, dev_pid, res);
 						}
 					}
 				} /* altsettings */