]> sigrok.org Git - libsigrok.git/commitdiff
zeroplus-logic-cube: move USB VID:PID check in scan before device access
authorGerhard Sittig <redacted>
Thu, 28 Sep 2023 18:53:26 +0000 (20:53 +0200)
committerGerhard Sittig <redacted>
Mon, 9 Oct 2023 16:40:05 +0000 (18:40 +0200)
Getting USB strings from faulty devices can be troublesome. Users report
segfaults when unrelated(!) devices get accessed during scan for certain
measurement devices.

Move the VID:PID check during scan before the USB string access. This
avoids access to unrelated devices.

Rephrase the check whether a USB device is a supported model. The
previous implementation's operation is questioned, the loop condition
looked suspicious.

Reported-By: Marek Antoniak <redacted> [the early check]
src/hardware/zeroplus-logic-cube/api.c

index 9d15fe6c97c13ec7e915e3036861ba348e228f30..1fae2c8a8f541e00788e7d19cf03e69ff18e46b4 100644 (file)
@@ -170,6 +170,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        libusb_device **devlist;
        GSList *devices;
        int ret, i, j;
+       const struct zp_model *check;
        char serial_num[64], connection_id[64];
 
        (void)options;
@@ -184,6 +185,30 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        for (i = 0; devlist[i]; i++) {
                libusb_get_device_descriptor(devlist[i], &des);
 
+               /*
+                * Check for expected VID:PID first as soon as we got
+                * the descriptor's content. This avoids access to flaky
+                * unrelated devices which trouble the application even
+                * if they are unrelated to measurement purposes.
+                *
+                * See https://sigrok.org/bugzilla/show_bug.cgi?id=1115
+                * and https://github.com/sigrokproject/libsigrok/pull/165
+                * for a discussion.
+                */
+               prof = NULL;
+               for (j = 0; zeroplus_models[j].vid; j++) {
+                       check = &zeroplus_models[j];
+                       if (des.idVendor != check->vid)
+                               continue;
+                       if (des.idProduct != check->pid)
+                               continue;
+                       prof = check;
+                       break;
+               }
+               if (!prof)
+                       continue;
+
+               /* Get the device's serial number from USB strings. */
                if ((ret = libusb_open(devlist[i], &hdl)) < 0)
                        continue;
 
@@ -202,16 +227,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
                        continue;
 
-               prof = NULL;
-               for (j = 0; j < zeroplus_models[j].vid; j++) {
-                       if (des.idVendor == zeroplus_models[j].vid &&
-                               des.idProduct == zeroplus_models[j].pid) {
-                               prof = &zeroplus_models[j];
-                       }
-               }
-
-               if (!prof)
-                       continue;
                sr_info("Found ZEROPLUS %s.", prof->model_name);
 
                sdi = g_malloc0(sizeof(struct sr_dev_inst));