Linux clock framework development
 help / color / mirror / Atom feed
From: chenyuan_fl@163.com
To: kuninori.morimoto.gx@renesas.com, geert+renesas@glider.be,
	mturquette@baylibre.com, sboyd@kernel.org
Cc: linux-renesas-soc@vger.kernel.org, linux-clk@vger.kernel.org,
	linux-kernel@vger.kernel.org, Yuan CHen <chenyuan@kylinos.cn>
Subject: [PATCH v2] clk: renesas: fix memory leak in cpg_mssr_reserved_init()
Date: Mon,  8 Sep 2025 02:28:10 +0100	[thread overview]
Message-ID: <20250908012810.4767-1-chenyuan_fl@163.com> (raw)
In-Reply-To: <87segx256a.wl-kuninori.morimoto.gx@renesas.com>

From: Yuan CHen <chenyuan@kylinos.cn>

In the current implementation, when krealloc_array() fails, the error handling path
incorrectly sets the original memory pointer to NULL before it is freed,
resulting in a memory leak during reallocation failure.

Fixes: 6aa17547649 ("clk: renesas: cpg-mssr: Ignore all clocks assigned to non-Linux system")
Signed-off-by: Yuan CHen <chenyuan@kylinos.cn>
---
 drivers/clk/renesas/renesas-cpg-mssr.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/renesas/renesas-cpg-mssr.c b/drivers/clk/renesas/renesas-cpg-mssr.c
index 5ff6ee1f7d4b..de1cf7ba45b7 100644
--- a/drivers/clk/renesas/renesas-cpg-mssr.c
+++ b/drivers/clk/renesas/renesas-cpg-mssr.c
@@ -1082,6 +1082,7 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 
 		of_for_each_phandle(&it, rc, node, "clocks", "#clock-cells", -1) {
 			int idx;
+			unsigned int *new_ids;
 
 			if (it.node != priv->np)
 				continue;
@@ -1092,11 +1093,13 @@ static int __init cpg_mssr_reserved_init(struct cpg_mssr_priv *priv,
 			if (args[0] != CPG_MOD)
 				continue;
 
-			ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
-			if (!ids) {
+			new_ids = krealloc_array(ids, (num + 1), sizeof(*ids), GFP_KERNEL);
+			if (!new_ids) {
 				of_node_put(it.node);
+				kfree(ids);
 				return -ENOMEM;
 			}
+			ids = new_ids;
 
 			if (priv->reg_layout == CLK_REG_LAYOUT_RZ_A)
 				idx = MOD_CLK_PACK_10(args[1]);	/* for DEF_MOD_STB() */
-- 
2.39.5


  reply	other threads:[~2025-09-08  1:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05  1:57 [PATCH] clk: renesas: fix memory leak in cpg_mssr_reserved_init() chenyuan_fl
2025-09-07 23:50 ` Kuninori Morimoto
2025-09-08  1:28   ` chenyuan_fl [this message]
2025-09-08  2:02     ` [PATCH v2] " Kuninori Morimoto
2025-09-10  9:27       ` Geert Uytterhoeven
2025-09-15 13:12   ` [PATCH] " chenyuan_fl
2025-09-15 23:51     ` Kuninori Morimoto
2025-09-15 23:56     ` Kuninori Morimoto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250908012810.4767-1-chenyuan_fl@163.com \
    --to=chenyuan_fl@163.com \
    --cc=chenyuan@kylinos.cn \
    --cc=geert+renesas@glider.be \
    --cc=kuninori.morimoto.gx@renesas.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=sboyd@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox