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 <redacted>
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)
libusb_close(dev_hdl);
sdi = lascar_identify(config);
}
+ libusb_free_device_list(devlist, 1);
return sdi;
}