From ce5eef6530095961ad52e3b9974c8ad60579cacf Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Fri, 14 Aug 2020 11:05:42 +0100 Subject: [PATCH] shared/udev-util: fix sd_device leak in device_wait_for_initialization If the caller doesn't pass a return pointer, or if sd_event_loop fails after the device was found and referenced, it never gets dereferenced. --- src/shared/udev-util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/shared/udev-util.c b/src/shared/udev-util.c index eb79bbbcf06..8b27a6d0d03 100644 --- a/src/shared/udev-util.c +++ b/src/shared/udev-util.c @@ -138,6 +138,12 @@ struct DeviceMonitorData { sd_device *device; }; +static void device_monitor_data_free(struct DeviceMonitorData *d) { + assert(d); + + sd_device_unref(d->device); +} + static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) { struct DeviceMonitorData *data = userdata; const char *sysname; @@ -183,7 +189,7 @@ static int device_wait_for_initialization_internal( _cleanup_(sd_event_unrefp) sd_event *event = NULL; /* Ensure that if !_device && devlink, device gets unrefd on errors since it will be new */ _cleanup_(sd_device_unrefp) sd_device *device = sd_device_ref(_device); - struct DeviceMonitorData data = { + _cleanup_(device_monitor_data_free) struct DeviceMonitorData data = { .devlink = devlink, }; int r; -- 2.47.3