[Concept,05/35] mmc: sandbox: Fix memory leak on probe failure

Message ID 20251210000737.180797-6-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:06 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

If mmc_init() fails, the buffer allocated by calloc() or mapped by
os_map_file() is not freed.

Free or unmap the buffer before returning the error.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 drivers/mmc/sandbox_mmc.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
  

Patch

diff --git a/drivers/mmc/sandbox_mmc.c b/drivers/mmc/sandbox_mmc.c
index a24520f2e78..f49cb4b146a 100644
--- a/drivers/mmc/sandbox_mmc.c
+++ b/drivers/mmc/sandbox_mmc.c
@@ -190,7 +190,16 @@  static int sandbox_mmc_probe(struct udevice *dev)
 		}
 	}
 
-	return mmc_init(&plat->mmc);
+	ret = mmc_init(&plat->mmc);
+	if (ret) {
+		if (plat->fname)
+			os_unmap(priv->buf, priv->size);
+		else
+			free(priv->buf);
+		return ret;
+	}
+
+	return 0;
 }
 
 static int sandbox_mmc_remove(struct udevice *dev)