iio: gts-helper: Fix memory leaks for the error path of iio_gts_build_avail_scale_table()
Subsystems (2)
PATCH v2 1/1 iio: gts-helper: Fix memory leaks for the error path of iio_gts_build_avail_scale_table()
If per_time_scales[i] or per_time_gains[i] kcalloc fails in the for loop of iio_gts_build_avail_scale_table(), the err_free_out will fail to call kfree() each time when i is reduced to 0, so all the per_time_scales[0] and per_time_gains[0] will not be freed, which will cause memory leaks.
Fix it by checking if i >= 0.
---
v2:
- Update the commit message.
- Add Reviewed-by.
--- drivers/iio/industrialio-gts-helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +1 -1
@@ -315,7 +315,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)return 0 ; err_free_out: -for (i--; i; i--) { +for (i--; i >=0 ; i--) {kfree (per_time_scales[i]);kfree (per_time_gains[i]); }
Signature · 1 line
2.34.1
Quoted 11 lines · On Wed, 16 Oct 2024 09:24:53 +0800 Jinjie Ruan <[email protected]> wrote:
If per_time_scales[i] or per_time_gains[i] kcalloc fails in the for loop of
iio_gts_build_avail_scale_table(), theerr_free_outwill fail to callkfree()each time when i is reduced to 0, so all the per_time_scales[0] and per_time_gains[0] will not be freed, which will cause memory leaks.Fix it by checking if i >= 0.
Fixes 38416c28e168 ("iio: light: Add gain-time-scale helpers")Reviewed-by Matti Vaittinen <[email protected]>Signed-off-by Jinjie Ruan <[email protected]>
Applied.
Quoted 21 lines
---
v2:
- Update the commit message.
- Add Reviewed-by.--- drivers/iio/industrialio-gts-helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iio/industrialio-gts-helper.c +1
-1@@ -315,7 +315,7 @@ static int iio_gts_build_avail_scale_table(struct iio_gts *gts)return 0 ; err_free_out: -for (i--; i; i--) { +for (i--; i >=0 ; i--) {kfree (per_time_scales[i]);kfree (per_time_gains[i]); }