]> sigrok.org Git - libsigrok.git/commitdiff
victor-dmm: Fix USB device list leak
authorLars-Peter Clausen <redacted>
Fri, 6 May 2016 11:26:02 +0000 (13:26 +0200)
committerLars-Peter Clausen <redacted>
Mon, 9 May 2016 12:17:08 +0000 (14:17 +0200)
The victor-dmm driver uses libusb_get_device_list() but neglects to call
the matching libusb_device_list_free() on the error path of libusb_open().
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/victor-dmm/api.c

index aefbe8b2da24ed1f0b85828c7c46cb47cf27de2b..47854ee929d75dbfe8a0d57885c6d917264ffa25 100644 (file)
@@ -107,28 +107,13 @@ static int dev_open(struct sr_dev_inst *sdi)
        struct sr_dev_driver *di = sdi->driver;
        struct drv_context *drvc = di->context;
        struct sr_usb_dev_inst *usb;
-       libusb_device **devlist;
-       int ret, i;
-       char connection_id[64];
+       int ret;
 
        usb = sdi->conn;
 
-       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
-       for (i = 0; devlist[i]; i++) {
-               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
-               if (strcmp(sdi->connection_id, connection_id))
-                       continue;
-               if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
-                       sr_err("Failed to open device: %s.", libusb_error_name(ret));
-                       return SR_ERR;
-               }
-               break;
-       }
-       libusb_free_device_list(devlist, 1);
-       if (!devlist[i]) {
-               sr_err("Device not found.");
-               return SR_ERR;
-       }
+       ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
+       if (ret != SR_OK)
+               return ret;
 
        /* The device reports as HID class, so the kernel would have
         * claimed it. */