Skip to contentSkip to conversation

edd: Fix memory leak in edd_init

PATCH New Miaoqian Lin 1 participant 1 message Jan 20, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 edd: Fix memory leak in edd_init

edd_init calls edd_device_register(), which calls kobject_init_and_add() and propagates the error code to the caller.
And kobject_init_and_add() takes reference even when it fails. According to the doc of kobject_init_and_add():

If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object.

Fix the error handling by adding kobject_put(). And callback function edd_release() in kobject_put() calls kfree(dev). So remove the redundant kfree(dev);

---
 drivers/firmware/edd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/edd.c +2 -2
index 69353dd0ea22..1135e66f92bc 100644
@@ -748,8 +748,8 @@ edd_init(void)
 		}
 
 		rc = edd_device_register(edev, i);
-		if (rc) {
-			kfree(edev);
+		if (rc < 0) {
+			kobject_put(&edev->kobj);
 			goto out;
 		}
 		edd_devices[i] = edev;
Signature · 1 line
2.17.1