When processing list of USB devices, avoid allocating memory
for devices which later will be skipped. Use existing libusb
struct first to check whether the device shall be excluded.

Signed-off-by: Yuri Benditovich <yuri.benditov...@daynix.com>
---
 src/win-usb-dev.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/win-usb-dev.c b/src/win-usb-dev.c
index bb11e8b..b3b2ed8 100644
--- a/src/win-usb-dev.c
+++ b/src/win-usb-dev.c
@@ -97,7 +97,7 @@ static void g_udev_device_print_list(GList *l, const gchar 
*msg) {}
 #endif
 static void g_udev_device_print(GUdevDevice *udev, const gchar *msg);
 
-static gboolean g_udev_skip_search(GUdevDevice *udev);
+static gboolean g_udev_skip_search(libusb_device *dev);
 
 GQuark g_udev_client_error_quark(void)
 {
@@ -152,13 +152,12 @@ g_udev_client_list_devices(GUdevClient *self, GList 
**devs,
 
     n = 0;
     for (dev = lusb_list; *dev; dev++) {
+        if (g_udev_skip_search(*dev)) {
+            continue;
+        }
         udevinfo = g_new0(GUdevDeviceInfo, 1);
         get_usb_dev_info(*dev, udevinfo);
         udevice = g_udev_device_new(udevinfo);
-        if (g_udev_skip_search(udevice)) {
-            g_object_unref(udevice);
-            continue;
-        }
         *devs = g_list_prepend(*devs, udevice);
         n++;
     }
@@ -549,19 +548,17 @@ static void g_udev_device_print(GUdevDevice *udev, const 
gchar *msg)
                 udevinfo->vid, udevinfo->pid, udevinfo->class);
 }
 
-static gboolean g_udev_skip_search(GUdevDevice *udev)
+static gboolean g_udev_skip_search(libusb_device *dev)
 {
-    GUdevDeviceInfo* udevinfo;
     gboolean skip;
+    uint8_t addr  = libusb_get_device_address(dev);
+    struct libusb_device_descriptor desc;
 
-    g_return_val_if_fail(G_UDEV_DEVICE(udev), FALSE);
-
-    udevinfo = udev->priv->udevinfo;
-    g_return_val_if_fail(udevinfo != NULL, FALSE);
+    libusb_get_device_descriptor(dev, &desc);
 
-    skip = ((udevinfo->addr == 0xff) ||  /* root hub (HCD) */
-            (udevinfo->addr == 1) || /* root hub addr */
-            (udevinfo->class == LIBUSB_CLASS_HUB) || /* hub*/
-            (udevinfo->addr == 0)); /* bad address */
+    skip = ((addr == 0xff) ||  /* root hub (HCD) */
+            (addr == 1) || /* root hub addr */
+            (desc.bDeviceClass == LIBUSB_CLASS_HUB) || /* hub*/
+            (addr == 0)); /* bad address */
     return skip;
 }
-- 
2.17.1

_______________________________________________
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/spice-devel

Reply via email to