[PATCH] EDAC/mc: fix potential memoryleak in edac_mc_alloc()

From: Miaohe Lin
Date: Sat Jun 03 2023 - 02:54:05 EST


When fails to allocate memory for layers or pvt_info, _edac_mc_free()
will be called to release the resource of edac mc but mci->dev is not
even initialized at that time. _edac_mc_free() will fail to release
the allocated memory.

Fixes: 0bbb265f7089 ("EDAC/mc: Get rid of silly one-shot struct allocation in edac_mc_alloc()")
Signed-off-by: Miaohe Lin <linmiaohe@xxxxxxxxxx>
---
drivers/edac/edac_mc.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
index 6faeb2ab3960..89dc5954d19e 100644
--- a/drivers/edac/edac_mc.c
+++ b/drivers/edac/edac_mc.c
@@ -364,6 +364,8 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
mci = kzalloc(sizeof(struct mem_ctl_info), GFP_KERNEL);
if (!mci)
return NULL;
+ mci->dev.release = mci_release;
+ device_initialize(&mci->dev);

mci->layers = kcalloc(n_layers, sizeof(struct edac_mc_layer), GFP_KERNEL);
if (!mci->layers)
@@ -373,9 +375,6 @@ struct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
if (!mci->pvt_info)
goto error;

- mci->dev.release = mci_release;
- device_initialize(&mci->dev);
-
/* setup index and various internal pointers */
mci->mc_idx = mc_num;
mci->tot_dimms = tot_dimms;
--
2.27.0