]> sigrok.org Git - libsigrok.git/commitdiff
zeroplus-logic-cube: Fix USB device list
authorLars-Peter Clausen <redacted>
Fri, 6 May 2016 11:27:09 +0000 (13:27 +0200)
committerLars-Peter Clausen <redacted>
Mon, 9 May 2016 12:17:33 +0000 (14:17 +0200)
The zeroplus-logic-cube driver uses libusb_get_device_list() but neglects
to call the matching libusb_device_list_free() on the error path. This will
leak the memory allocated for the list as well as all the devices.

To address the issue use sr_usb_open() instead of open-coding its
functionality. sr_usb_open() correctly handles freeing the device list.

The issue was discovered using the following coccinelle semantic patch:
// <smpl>
@@
identifier devlist;
expression ctx, ret;
statement S;
@@
(
 libusb_get_device_list(ctx, &devlist);
|
 ret = libusb_get_device_list(ctx, &devlist);
 if (ret < 0) S
)
... when != libusb_free_device_list(devlist, ...)
*return ...;
// </smpl>

Signed-off-by: Lars-Peter Clausen <redacted>
src/hardware/zeroplus-logic-cube/api.c

index 1081b81c2b461ec4cf059a9f8f0dc8c6df80dfd5..23a2e69f986c75d88deffa0c4ec37bb7ea576d85 100644 (file)
@@ -262,43 +262,17 @@ static int dev_open(struct sr_dev_inst *sdi)
        struct dev_context *devc;
        struct drv_context *drvc;
        struct sr_usb_dev_inst *usb;
-       libusb_device **devlist, *dev;
-       int device_count, ret, i;
-       char connection_id[64];
+       int ret;
 
        drvc = di->context;
        usb = sdi->conn;
        devc = sdi->priv;
 
-       device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
-                                             &devlist);
-       if (device_count < 0) {
-               sr_err("Failed to retrieve device list.");
-               return SR_ERR;
-       }
+       ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
+       if (ret != SR_OK)
+               return ret;
 
-       dev = NULL;
-       for (i = 0; i < device_count; i++) {
-               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
-               if (!strcmp(sdi->connection_id, connection_id)) {
-                       dev = devlist[i];
-                       break;
-               }
-       }
-       if (!dev) {
-               sr_err("Device on %d.%d (logical) / %s (physical) disappeared!",
-                      usb->bus, usb->address, sdi->connection_id);
-               return SR_ERR;
-       }
-
-       if (!(ret = libusb_open(dev, &(usb->devhdl)))) {
-               sdi->status = SR_ST_ACTIVE;
-               sr_info("Opened device on %d.%d (logical) / %s (physical) interface %d.",
-                       usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
-       } else {
-               sr_err("Failed to open device: %s.", libusb_error_name(ret));
-               return SR_ERR;
-       }
+       sdi->status = SR_ST_ACTIVE;
 
        ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
        if (ret < 0) {