The testo 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 <l...@metafoo.de>
---
 src/hardware/zeroplus-logic-cube/api.c | 36 +++++-----------------------------
 1 file changed, 5 insertions(+), 31 deletions(-)

diff --git a/src/hardware/zeroplus-logic-cube/api.c 
b/src/hardware/zeroplus-logic-cube/api.c
index 1081b81..23a2e69 100644
--- a/src/hardware/zeroplus-logic-cube/api.c
+++ b/src/hardware/zeroplus-logic-cube/api.c
@@ -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) {
-- 
2.1.4


------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to