From: Gerhard Sittig Date: Thu, 28 Sep 2023 18:53:26 +0000 (+0200) Subject: zeroplus-logic-cube: move USB VID:PID check in scan before device access X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=fd86380b48f1abff31f523e33c7c12280be1c1f7 zeroplus-logic-cube: move USB VID:PID check in scan before device access 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 [the early check] --- diff --git a/src/hardware/zeroplus-logic-cube/api.c b/src/hardware/zeroplus-logic-cube/api.c index 9d15fe6c..1fae2c8a 100644 --- a/src/hardware/zeroplus-logic-cube/api.c +++ b/src/hardware/zeroplus-logic-cube/api.c @@ -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));