diff mbox

hwmon: (core) fix resource leak on devm_kcalloc failure

Message ID 20161023205608.11867-1-colin.king@canonical.com (mailing list archive)
State Accepted
Headers show

Commit Message

Colin King Oct. 23, 2016, 8:56 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

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 <colin.king@canonical.com>
---
 drivers/hwmon/hwmon.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Guenter Roeck Oct. 24, 2016, 1:09 p.m. UTC | #1
On 10/23/2016 01:56 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> 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 <colin.king@canonical.com>

Good catch! Applied.

Thanks,
Guenter

> ---
>  drivers/hwmon/hwmon.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> index adae684..a74c075 100644
> --- a/drivers/hwmon/hwmon.c
> +++ b/drivers/hwmon/hwmon.c
> @@ -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)) {
>

--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index adae684..a74c075 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -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)) {