2022-04-07 20:48:00

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH] clk: hi3620: fix potential memory leak in hi3620_mmc_clk_init()

From: Xiaoke Wang <[email protected]>

In hi3620_mmc_clk_init(), if `clk_data->clks` which is allocated by
kcalloc() fails, it will directly return without releasing `clk_data`
that is allocated by kzalloc(), which may lead to memory leak.
So this patch added kfree(clk_data) to the above error path before
returning.

Besides, `base` is mapped by of_iomap(). However, two error paths ignore
to unmap it. Therefore, this patch also added iounmap(map) to these two
error paths.

Signed-off-by: Xiaoke Wang <[email protected]>
---
drivers/clk/hisilicon/clk-hi3620.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/hisilicon/clk-hi3620.c b/drivers/clk/hisilicon/clk-hi3620.c
index a3d04c7..bdd36eb 100644
--- a/drivers/clk/hisilicon/clk-hi3620.c
+++ b/drivers/clk/hisilicon/clk-hi3620.c
@@ -464,11 +464,11 @@ static void __init hi3620_mmc_clk_init(struct device_node *node)

clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
if (WARN_ON(!clk_data))
- return;
+ goto unmap_base;

clk_data->clks = kcalloc(num, sizeof(*clk_data->clks), GFP_KERNEL);
if (!clk_data->clks)
- return;
+ goto free_clk_data;

for (i = 0; i < num; i++) {
struct hisi_mmc_clock *mmc_clk = &hi3620_mmc_clks[i];
@@ -478,6 +478,12 @@ static void __init hi3620_mmc_clk_init(struct device_node *node)

clk_data->clk_num = num;
of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
+ return;
+
+free_clk_data:
+ kfree(clk_data);
+unmap_base:
+ iounmap(base);
}

CLK_OF_DECLARE(hi3620_mmc_clk, "hisilicon,hi3620-mmc-clock", hi3620_mmc_clk_init);
--