zeroplus-logic-cube: Fix USB device list

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 <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2016-05-06 13:27:09 +02:00
parent 4af1f68f9a
commit 87629577fe
1 changed files with 5 additions and 31 deletions

View 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) {