From 93db93a0ad6ff058d45ef1cccd3a7ece800a5aa4 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Sun, 6 Jun 2021 13:03:59 +0200 Subject: [PATCH 1/2] fix for 'libusb_devices were leaked' when no stlink was found --- src/stlink-lib/usb.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/stlink-lib/usb.c b/src/stlink-lib/usb.c index 2754db06f..68e6302b9 100644 --- a/src/stlink-lib/usb.c +++ b/src/stlink-lib/usb.c @@ -1273,6 +1273,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, if (cnt < 0) { WLOG ("Couldn't find %s ST-Link devices\n", (devBus && devAddr) ? "matched" : "any"); + libusb_free_device_list(list, 1); goto on_error; } else { ret = libusb_open(list[cnt], &slu->usb_handle); From ca896108eedde4a1a4c1ce00a1917771757bf514 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Tue, 15 Jun 2021 20:45:19 +0200 Subject: [PATCH 2/2] fix for when the device list was not initialized --- src/stlink-lib/usb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stlink-lib/usb.c b/src/stlink-lib/usb.c index 68e6302b9..9f83ace4a 100644 --- a/src/stlink-lib/usb.c +++ b/src/stlink-lib/usb.c @@ -1203,7 +1203,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, libusb_set_option(slu->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, ugly_libusb_log_level(verbose)); #endif - libusb_device **list; + libusb_device **list = NULL; // TODO: We should use ssize_t and use it as a counter if > 0. // As per libusb API: ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list) int cnt = (int)libusb_get_device_list(slu->libusb_ctx, &list); @@ -1232,7 +1232,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, ILOG("bus %03d dev %03d\n", devBus, devAddr); } - while (cnt--) { + while (cnt-- > 0) { struct libusb_device_handle *handle; libusb_get_device_descriptor(list[cnt], &desc);