>From 076666855d9a9c481ae4c983462e0d3afef92fb7 Mon Sep 17 00:00:00 2001 From: David Allan Date: Fri, 28 May 2010 22:22:05 -0400 Subject: [PATCH 1/1] Fix leaks in udev device add/remove * This patch is a modification of a patch submitted by Nigel Jones. It fixes several memory leaks on device addition/removal: 1. Free the virNodeDeviceDefPtr in udevAddOneDevice if the return value is non-zero 2. The node device reference must always be released after the device has been processed, so move the call to udev_device_unref into udevAddOneDevice and add a call to udev_device_unref to udevRemoveOneDevice --- src/node_device/node_device_udev.c | 12 ++++++++++-- 1 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 6e3ecd7..5193f5b 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -1211,6 +1211,8 @@ static int udevRemoveOneDevice(struct udev_device *device) } nodeDeviceUnlock(driverState); + udev_device_unref(device); + return ret; } @@ -1309,13 +1311,14 @@ static int udevAddOneDevice(struct udev_device *device) goto out; } + /* If this is a device change, the old definition will be freed + * and the current definition will take its place. */ nodeDeviceLock(driverState); dev = virNodeDeviceAssignDef(&driverState->devs, def); nodeDeviceUnlock(driverState); if (dev == NULL) { VIR_ERROR(_("Failed to create device for '%s'"), def->name); - virNodeDeviceDefFree(def); goto out; } @@ -1324,6 +1327,12 @@ static int udevAddOneDevice(struct udev_device *device) ret = 0; out: + if (ret != 0) { + virNodeDeviceDefFree(def); + } + + udev_device_unref(device); + return ret; } @@ -1343,7 +1352,6 @@ static int udevProcessDeviceListEntry(struct udev *udev, VIR_INFO("Failed to create node device for udev device '%s'", name); } - udev_device_unref(device); ret = 0; } -- 1.7.0.1