[PATCH] edd: Fix memory leak in edd_init

From: Miaoqian Lin
Date: Thu Jan 20 2022 - 06:14:05 EST


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);

Signed-off-by: Miaoqian Lin <linmq006@xxxxxxxxx>
---
drivers/firmware/edd.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/edd.c b/drivers/firmware/edd.c
index 69353dd0ea22..1135e66f92bc 100644
--- a/drivers/firmware/edd.c
+++ b/drivers/firmware/edd.c
@@ -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;
--
2.17.1