From 8bef41c8602929dca074194d8701302a55f86b96 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Mon, 20 Nov 2023 14:48:35 +0000 Subject: [PATCH] Close the libusb handle if returning failure from open() --- gusb/gusb-device.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gusb/gusb-device.c b/gusb/gusb-device.c index a9cb63a..d52c402 100644 --- a/gusb/gusb-device.c +++ b/gusb/gusb-device.c @@ -863,7 +863,15 @@ _g_usb_device_open_internal(GUsbDevice *self, GError **error) /* open device */ rc = libusb_open(priv->device, &priv->handle); - return g_usb_device_libusb_error_to_gerror(self, rc, error); + if (!g_usb_device_libusb_error_to_gerror(self, rc, error)) { + if (priv->handle != NULL) + libusb_close(priv->handle); + priv->handle = NULL; + return FALSE; + } + + /* success */ + return TRUE; } /**