From: Lars-Peter Clausen Date: Fri, 6 May 2016 11:22:19 +0000 (+0200) Subject: lascar-el-usb: lascar_scan(): Fix USB device list leak X-Git-Tag: libsigrok-0.5.0~428 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=ceb2da179f9446b0e61bde7cf7be9133b25683cc;p=libsigrok.git lascar-el-usb: lascar_scan(): Fix USB device list leak 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: // @@ 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 ...; // Signed-off-by: Lars-Peter Clausen --- diff --git a/src/hardware/lascar-el-usb/protocol.c b/src/hardware/lascar-el-usb/protocol.c index e4fae68f..f7b053f3 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; }