hwmon: nct6683: fix memory leak in nct6683_create_attr_group
PATCH 1/1 hwmon: nct6683: fix memory leak in nct6683_create_attr_group
If allocation fails, free prior allocations before returning.
Not tested on real hardware. Did not update rest of file for style changes, only updated the NULL conditional checks.
--- drivers/hwmon/nct6683.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/hwmon/nct6683.c +11 -5
@@ -431,18 +431,18 @@ nct6683_create_attr_group(struct device *dev,return ERR_PTR (-EINVAL ); group =devm_kzalloc (dev,sizeof (*group),GFP_KERNEL ); -if (group ==NULL ) +if (!group)return ERR_PTR (-ENOMEM ); attrs =devm_kcalloc (dev, repeat * count +1 ,sizeof (*attrs),GFP_KERNEL ); -if (attrs ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!attrs) + goto out_free_group; su =devm_kzalloc (dev,array3_size (repeat, count,sizeof (*su)),GFP_KERNEL ); -if (su ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!su) + goto out_free_attrs; group->attrs = attrs; group->is_visible = tg->is_visible;
@@ -481,6 +481,12 @@ nct6683_create_attr_group(struct device *dev, }return group; + +out_free_attrs: +devm_kfree (dev, attrs); +out_free_group: +devm_kfree (dev, group); +return ERR_PTR (-ENOMEM ); }/* LSB is 16 mV, except for the following sources, where it is 32 mV */
Signature · 1 line
2.51.0
Guenter Roeck
maintainerQuoted 6 lines · Joshua Peisach
If allocation fails, free prior allocations before returning.
Not tested on real hardware. Did not update rest of file for style changes, only updated the NULL conditional checks.
Signed-off-by Joshua Peisach <[email protected]>
Allocation is with devm_kzalloc(), so the memory is released after the driver is unloaded, which happens immediately because the probe function is about to fail.
Quoted 33 lines
--- drivers/hwmon/nct6683.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)drivers/hwmon/nct6683.c +5
-5@@ -431,18 +431,18 @@ nct6683_create_attr_group(struct device *dev,return ERR_PTR (-EINVAL ); group =devm_kzalloc (dev,sizeof (*group),GFP_KERNEL ); -if (group ==NULL ) +if (!group)return ERR_PTR (-ENOMEM ); attrs =devm_kcalloc (dev, repeat * count +1 ,sizeof (*attrs),GFP_KERNEL ); -if (attrs ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!attrs) + goto out_free_group; su =devm_kzalloc (dev,array3_size (repeat, count,sizeof (*su)),GFP_KERNEL ); -if (su ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!su) + goto out_free_attrs; group->attrs = attrs; group->is_visible = tg->is_visible;
drivers/hwmon/nct6683.c nct6683_create_attr_group(struct device *dev, · line 488
Quoted 8 lines
drivers/hwmon/nct6683.c +5
-0@@ -481,6 +481,12 @@ nct6683_create_attr_group(struct device *dev, }return group; + +out_free_attrs: +devm_kfree (dev, attrs); +out_free_group: +devm_kfree (dev, group);
That defeats the purpose if using devm_kzalloc().
NACK. There is no problem.
Guenter
Quoted 4 lines
drivers/hwmon/nct6683.c +1
-0+return ERR_PTR (-ENOMEM ); }/* LSB is 16 mV, except for the following sources, where it is 32 mV */
Quoted 56 lines · Guenter Roeck
Quoted 6 lines · Joshua Peisach
If allocation fails, free prior allocations before returning.
Not tested on real hardware. Did not update rest of file for style changes, only updated the NULL conditional checks.
Signed-off-by Joshua Peisach <[email protected]>Allocation is with
devm_kzalloc(), so the memory is released after the driver is unloaded, which happens immediately because the probe function is about to fail.Quoted 33 lines
--- drivers/hwmon/nct6683.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)drivers/hwmon/nct6683.c +5
-5@@ -431,18 +431,18 @@ nct6683_create_attr_group(struct device *dev,return ERR_PTR (-EINVAL ); group =devm_kzalloc (dev,sizeof (*group),GFP_KERNEL ); -if (group ==NULL ) +if (!group)return ERR_PTR (-ENOMEM ); attrs =devm_kcalloc (dev, repeat * count +1 ,sizeof (*attrs),GFP_KERNEL ); -if (attrs ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!attrs) + goto out_free_group; su =devm_kzalloc (dev,array3_size (repeat, count,sizeof (*su)),GFP_KERNEL ); -if (su ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!su) + goto out_free_attrs; group->attrs = attrs; group->is_visible = tg->is_visible;drivers/hwmon/nct6683.c nct6683_create_attr_group(struct device *dev, · line 488
Quoted 8 lines
drivers/hwmon/nct6683.c +5
-0@@ -481,6 +481,12 @@ nct6683_create_attr_group(struct device *dev, }return group; + +out_free_attrs: +devm_kfree (dev, attrs); +out_free_group: +devm_kfree (dev, group);That defeats the purpose if using
devm_kzalloc().
Quoted 2 lines
NACK. There is no problem.
Ah - I didn't see that was how devm_kzalloc works. Should I write some documentation in devres.h to clarify its behavior, so this doesn't happen again? Once I found it in the header it made sense :)
--
Josh
Quoted 6 lines
Guenter
Quoted 4 lines
drivers/hwmon/nct6683.c +1
-0+return ERR_PTR (-ENOMEM ); }/* LSB is 16 mV, except for the following sources, where it is 32 mV */
Guenter Roeck
maintainerQuoted 52 lines · Joshua Peisach
Quoted 51 lines · Guenter Roeck
Quoted 6 lines · Joshua Peisach
If allocation fails, free prior allocations before returning.
Not tested on real hardware. Did not update rest of file for style changes, only updated the NULL conditional checks.
Signed-off-by Joshua Peisach <[email protected]>Allocation is with
devm_kzalloc(), so the memory is released after the driver is unloaded, which happens immediately because the probe function is about to fail.Quoted 29 lines
--- drivers/hwmon/nct6683.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)drivers/hwmon/nct6683.c +5
-5@@ -431,18 +431,18 @@ nct6683_create_attr_group(struct device *dev,return ERR_PTR (-EINVAL ); group =devm_kzalloc (dev,sizeof (*group),GFP_KERNEL ); -if (group ==NULL ) +if (!group)return ERR_PTR (-ENOMEM ); attrs =devm_kcalloc (dev, repeat * count +1 ,sizeof (*attrs),GFP_KERNEL ); -if (attrs ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!attrs) + goto out_free_group; su =devm_kzalloc (dev,array3_size (repeat, count,sizeof (*su)),GFP_KERNEL ); -if (su ==NULL ) -return ERR_PTR (-ENOMEM ); +if (!su) + goto out_free_attrs; group->attrs = attrs; group->is_visible = tg->is_visible;drivers/hwmon/nct6683.c nct6683_create_attr_group(struct device *dev, · line 487
Quoted 7 lines
drivers/hwmon/nct6683.c +5
-0@@ -481,6 +481,12 @@ nct6683_create_attr_group(struct device *dev, }return group; + +out_free_attrs: +devm_kfree (dev, attrs); +out_free_group: +devm_kfree (dev, group);That defeats the purpose if using
devm_kzalloc().
Quoted 7 lines
Quoted 2 lines
NACK. There is no problem.
Ah - I didn't see that was how
devm_kzallocworks. Should I write some documentation indevres.hto clarify its behavior, so this doesn't happen again? Once I found it in the header it made sense :)
The whole purpose of all devm_ functions is to avoid the need for cleanup. This is already explained in detail in driver-api/driver-model/devres.rst. While that document might benefit from an update (there are lots of devm_ functions which are not listed), I don't see the need for another document.
Thanks,
Guenter