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:
// <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:22:19 +02:00
parent 6266deb84d
commit ceb2da179f
1 changed files with 6 additions and 1 deletions

View File

@ -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;
}