fpi-device: Do not leak USB devices while iterating

To compute the device ports we walked up through the devices using
g_usb_device_get_parent(), but this is supposed to return a device with
transfer full, so we need to unref it when done with it.

To handle this nicely, use a mixture of autopointer's and g_set_object
to ensure we're doing the right thing when passing the ownership around.
This commit is contained in:
Marco Trevisan (Treviño)
2022-09-27 18:17:59 +02:00
parent 62f2f34655
commit 5d9fc8b3c8

View File

@@ -1718,7 +1718,7 @@ fpi_device_configure_wakeup (FpDevice *device, gboolean enabled)
case FP_DEVICE_TYPE_USB: case FP_DEVICE_TYPE_USB:
{ {
g_autoptr(GString) ports = NULL; g_autoptr(GString) ports = NULL;
GUsbDevice *dev, *parent; g_autoptr(GUsbDevice) dev = NULL;
const char *wakeup_command = enabled ? "enabled" : "disabled"; const char *wakeup_command = enabled ? "enabled" : "disabled";
guint8 bus; guint8 bus;
g_autofree gchar *sysfs_wakeup = NULL; g_autofree gchar *sysfs_wakeup = NULL;
@@ -1729,16 +1729,20 @@ fpi_device_configure_wakeup (FpDevice *device, gboolean enabled)
bus = g_usb_device_get_bus (priv->usb_device); bus = g_usb_device_get_bus (priv->usb_device);
/* Walk up, skipping the root hub. */ /* Walk up, skipping the root hub. */
dev = priv->usb_device; g_set_object (&dev, priv->usb_device);
while ((parent = g_usb_device_get_parent (dev))) while (TRUE)
{ {
g_autoptr(GUsbDevice) parent = g_usb_device_get_parent (dev);
g_autofree gchar *port_str = NULL; g_autofree gchar *port_str = NULL;
guint8 port; guint8 port;
if (!parent)
break;
port = g_usb_device_get_port_number (dev); port = g_usb_device_get_port_number (dev);
port_str = g_strdup_printf ("%d.", port); port_str = g_strdup_printf ("%d.", port);
g_string_prepend (ports, port_str); g_string_prepend (ports, port_str);
dev = parent; g_set_object (&dev, parent);
} }
g_string_set_size (ports, ports->len - 1); g_string_set_size (ports, ports->len - 1);