hwmon: (core) fix resource leak on devm_kcalloc failure
PATCH 1/1 hwmon: (core) fix resource leak on devm_kcalloc failure
From: Colin Ian King <[email protected]>
If dev_kcalloc fails to allocate hw_dev->groups then the current exit path is a direct return, causing a leak of resources such as hwdev and ida is not removed. Fix this by exiting via the free_hwmon exit path that performs the necessary resource cleanup.
Signed-off-by Colin Ian King <[email protected]>
--- drivers/hwmon/hwmon.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hwmon/hwmon.c +4 -2
@@ -536,8 +536,10 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, hwdev->groups =devm_kcalloc (dev, ngroups,sizeof (*groups),GFP_KERNEL ); -if (!hwdev->groups) -return ERR_PTR (-ENOMEM ); +if (!hwdev->groups) { + err = -ENOMEM ; + goto free_hwmon; + } attrs =__hwmon_create_attrs (dev, drvdata, chip);if (IS_ERR (attrs)) {
Signature · 1 line
2.9.3
Guenter Roeck
maintainerQuoted 8 lines · Colin King
From: Colin Ian King <[email protected]>
If
dev_kcallocfails to allocatehw_dev->groupsthen the current exit path is a direct return, causing a leak of resources such as hwdev and ida is not removed. Fix this by exiting via thefree_hwmonexit path that performs the necessary resource cleanup.Signed-off-by Colin Ian King <[email protected]>
Good catch! Applied.
Thanks,
Guenter
Quoted 22 lines
--- drivers/hwmon/hwmon.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/hwmon/hwmon.c +4
-2@@ -536,8 +536,10 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, hwdev->groups =devm_kcalloc (dev, ngroups,sizeof (*groups),GFP_KERNEL ); -if (!hwdev->groups) -return ERR_PTR (-ENOMEM ); +if (!hwdev->groups) { + err = -ENOMEM ; + goto free_hwmon; + } attrs =__hwmon_create_attrs (dev, drvdata, chip);if (IS_ERR (attrs)) {