[PATCH] clk: tegra: tegra124-emc: fix krealloc() memory leak
From: Alexander A. Klimov
Date: Tue May 26 2026 - 02:14:26 EST
Don't just overwrite the original pointer passed to krealloc()
with its return value without checking latter:
MEM = krealloc(MEM, SZ, GFP);
If krealloc() returns NULL, that erases the pointer
to the still allocated memory, hence leaks this memory.
Instead, use a temporary variable, check it's not NULL
and only then assign it to the original pointer:
TMP = krealloc(MEM, SZ, GFP);
if (!TMP) return;
MEM = TMP;
Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
Signed-off-by: Alexander A. Klimov <grandmaster@xxxxxxxxxxxx>
---
drivers/clk/tegra/clk-tegra124-emc.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/drivers/clk/tegra/clk-tegra124-emc.c b/drivers/clk/tegra/clk-tegra124-emc.c
index f3b2c96fdcfc..8053fbbb06c8 100644
--- a/drivers/clk/tegra/clk-tegra124-emc.c
+++ b/drivers/clk/tegra/clk-tegra124-emc.c
@@ -446,14 +446,13 @@ static int load_timings_from_dt(struct tegra_clk_emc *tegra,
struct emc_timing *timings_ptr;
int child_count = of_get_child_count(node);
int i = 0, err;
- size_t size;
+ size_t size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
+ void *mem = krealloc(tegra->timings, size, GFP_KERNEL);
- size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
-
- tegra->timings = krealloc(tegra->timings, size, GFP_KERNEL);
- if (!tegra->timings)
+ if (!mem)
return -ENOMEM;
+ tegra->timings = mem;
timings_ptr = tegra->timings + tegra->num_timings;
tegra->num_timings += child_count;
--
2.54.0