core: ensure that all devices are properly cleaned up on libusb_exit
authorNathan Hjelm <hjelmn@google.com>
Fri, 11 Jun 2021 02:51:44 +0000 (20:51 -0600)
committerNathan Hjelm <hjelmn@google.com>
Fri, 11 Jun 2021 02:56:24 +0000 (20:56 -0600)
commita0e773812b8aa07ad57fc092a7a4f406366f5272
tree832f6f2b1f7776fb2233b993a0b3f68be394b79e
parenta524555c987fec2c16417d2f58c8713efcbe11a9
core: ensure that all devices are properly cleaned up on libusb_exit

When cleaning up the context on libusb_exit the last step is to to call
hotplug_exit. This function took one pass over the devices and released any
devices where the reference count had reached 0. All remaining devices
were assumed to have leaked references and a warning message was printed
out. Unfortunately, this cleanup was too simplistic. It ignored the
references created when a device was the parent of another device (which
takes a reference). This reference is released when the child device is
released.

To ensure that we do not erroneously warn about devices with leftover
references the code now loops over the device list until no more devices
have a single reference. This ensures that we will eventually remove
all devices with no external references.

Fixes #924

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
libusb/hotplug.c
libusb/version_nano.h