Skip to contentSkip to conversation

mtd-ram: correctly free memory on error path in platram_probe()

PATCH Applied Baskov Evgeiny 2 participants 2 messages Nov 13, 2020 – Nov 19, 2020 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd-ram: correctly free memory on error path in platram_probe()

If an error happens in mtd_device_parse_register or mtd_device_register, memory allocated for struct platram_info is leaked.

Make platram_probe() call platram_remove() on all error paths after struct platram_info allocation to correctly free resources.

Found by Linux Driver Verification project (linuxtesting.org).

---
 drivers/mtd/maps/plat-ram.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/mtd/maps/plat-ram.c +8 -3
index 311742c78155..0bec7c791d17 100644
@@ -177,8 +177,12 @@ static int platram_probe(struct platform_device *pdev)
 	err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
 					pdata->partitions,
 					pdata->nr_partitions);
-	if (!err)
-		dev_info(&pdev->dev, "registered mtd device\n");
+	if (err) {
+		dev_err(&pdev->dev, "failed to register mtd device\n");
+		goto exit_free;
+	}
+
+	dev_info(&pdev->dev, "registered mtd device\n");
 
 	if (pdata->nr_partitions) {
 		/* add the whole device. */
@@ -186,10 +190,11 @@ static int platram_probe(struct platform_device *pdev)
 		if (err) {
 			dev_err(&pdev->dev,
 				"failed to register the entire device\n");
+			goto exit_free;
 		}
 	}
 
-	return err;
+	return 0;
 
  exit_free:
 	platram_remove(pdev);
Signature · 6 lines
2.29.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
maintainer Applied
#1 lore raw
Quoted 9 lines · Baskov Evgeiny

If an error happens in mtd_device_parse_register or mtd_device_register, memory allocated for struct platram_info is leaked.

Make platram_probe() call platram_remove() on all error paths after struct platram_info allocation to correctly free resources.

Found by Linux Driver Verification project (linuxtesting.org).

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git nand/next, thanks.

Miquel