lascar_scan() calls libusb_get_device_list() but never the matching
libusb_free_device_list(). This will leak the memory allocated for the
device list as well as all the devices. To fix this add the missing
libusb_free_device_list().

While we are at it also make sure to handle errors returned by
libusb_get_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 <[email protected]>
---
 src/hardware/lascar-el-usb/protocol.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/hardware/lascar-el-usb/protocol.c 
b/src/hardware/lascar-el-usb/protocol.c
index e4fae68..f7b053f 100644
--- a/src/hardware/lascar-el-usb/protocol.c
+++ b/src/hardware/lascar-el-usb/protocol.c
@@ -354,12 +354,16 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int 
address)
        struct libusb_device **devlist;
        libusb_device_handle *dev_hdl;
        int dummy, i;
+       int ret;
        unsigned char config[MAX_CONFIGBLOCK_SIZE];
 
        drvc = di->context;
        sdi = NULL;
 
-       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+       ret = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+       if (ret < 0)
+               return NULL;
+
        for (i = 0; devlist[i]; i++) {
                if (libusb_get_bus_number(devlist[i]) != bus ||
                                libusb_get_device_address(devlist[i]) != 
address)
@@ -374,6 +378,7 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int 
address)
                libusb_close(dev_hdl);
                sdi = lascar_identify(config);
        }
+       libusb_free_device_list(devlist, 1);
 
        return sdi;
 }
-- 
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to