1 From 369f05688911b05216cfcd6ca74473bec87948d7 Mon Sep 17 00:00:00 2001
2 From: Jinjie Ruan <ruanjinjie@huawei.com>
3 Date: Wed, 16 Oct 2024 09:24:53 +0800
4 Subject: iio: gts-helper: Fix memory leaks for the error path of iio_gts_build_avail_scale_table()
6 From: Jinjie Ruan <ruanjinjie@huawei.com>
8 commit 369f05688911b05216cfcd6ca74473bec87948d7 upstream.
10 If per_time_scales[i] or per_time_gains[i] kcalloc fails in the for loop
11 of iio_gts_build_avail_scale_table(), the err_free_out will fail to call
12 kfree() each time when i is reduced to 0, so all the per_time_scales[0]
13 and per_time_gains[0] will not be freed, which will cause memory leaks.
15 Fix it by checking if i >= 0.
17 Cc: stable@vger.kernel.org
18 Fixes: 38416c28e168 ("iio: light: Add gain-time-scale helpers")
19 Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
20 Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
21 Link: https://patch.msgid.link/20241016012453.2013302-1-ruanjinjie@huawei.com
22 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
23 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
25 drivers/iio/industrialio-gts-helper.c | 2 +-
26 1 file changed, 1 insertion(+), 1 deletion(-)
28 --- a/drivers/iio/industrialio-gts-helper.c
29 +++ b/drivers/iio/industrialio-gts-helper.c
30 @@ -313,7 +313,7 @@ static int iio_gts_build_avail_scale_tab
35 + for (i--; i >= 0; i--) {
36 kfree(per_time_scales[i]);
37 kfree(per_time_gains[i]);