lsusb-t: fix memory leak

The lists were never being cleaned up when finished, so properly walk
them and free the allocated the memory that was allocated.

Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Greg Kroah-Hartman 2024-10-01 20:08:38 +02:00
parent d04e63e551
commit a68705def3

View File

@ -717,6 +717,28 @@ static void print_tree(void)
}
}
static void cleanup(void)
{
struct usbdevice *device, *tempd;
struct usbinterface *interface, *templ;
struct usbbusnode *bus, *tempb;
list_for_each_safe(&usbdevlist, device, tempd, list) {
free(device);
}
list_for_each_safe(&interfacelist, interface, templ, list) {
free(interface);
}
bus = usbbuslist;
while (bus) {
tempb = bus->next;
free(bus);
bus = tempb;
}
}
int lsusb_t(void)
{
DIR *sbud = opendir(sys_bus_usb_devices);
@ -727,6 +749,7 @@ int lsusb_t(void)
sort_devices();
sort_busses();
print_tree();
cleanup();
} else
perror(sys_bus_usb_devices);
return sbud == NULL;