[PATCH] mtd: cfi: Use common error handling code in two functions

Markus Elfring Markus.Elfring at web.de
Wed Jun 10 00:21:46 PDT 2026


From: Markus Elfring <elfring at users.sourceforge.net>
Date: Wed, 10 Jun 2026 09:15:52 +0200

Use additional labels so that a bit of exception handling can be better
reused at the end of two function implementations.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring at users.sourceforge.net>
---
 drivers/mtd/chips/cfi_cmdset_0002.c | 13 +++++++------
 drivers/mtd/chips/cfi_cmdset_0020.c | 24 +++++++++++-------------
 2 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/drivers/mtd/chips/cfi_cmdset_0002.c b/drivers/mtd/chips/cfi_cmdset_0002.c
index a38aceb6612d..517db2f2707f 100644
--- a/drivers/mtd/chips/cfi_cmdset_0002.c
+++ b/drivers/mtd/chips/cfi_cmdset_0002.c
@@ -661,8 +661,7 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
 				       extp->MajorVersion, extp->MinorVersion,
 				       extp->MajorVersion, extp->MinorVersion);
 				kfree(extp);
-				kfree(mtd);
-				return NULL;
+				goto free_mtd;
 			}
 
 			printk(KERN_INFO "  Amd/Fujitsu Extended Query version %c.%c.\n",
@@ -714,10 +713,8 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
 		}
 		cfi_fixup(mtd, cfi_nopri_fixup_table);
 
-		if (!cfi->addr_unlock1 || !cfi->addr_unlock2) {
-			kfree(mtd);
-			return NULL;
-		}
+		if (!cfi->addr_unlock1 || !cfi->addr_unlock2)
+			goto free_mtd;
 
 	} /* CFI mode */
 	else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
@@ -755,6 +752,10 @@ struct mtd_info *cfi_cmdset_0002(struct map_info *map, int primary)
 	map->fldrv = &cfi_amdstd_chipdrv;
 
 	return cfi_amdstd_setup(mtd);
+
+free_mtd:
+	kfree(mtd);
+	return NULL;
 }
 struct mtd_info *cfi_cmdset_0006(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
 struct mtd_info *cfi_cmdset_0701(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0002")));
diff --git a/drivers/mtd/chips/cfi_cmdset_0020.c b/drivers/mtd/chips/cfi_cmdset_0020.c
index 6b5727eaae69..593ac65a7e2f 100644
--- a/drivers/mtd/chips/cfi_cmdset_0020.c
+++ b/drivers/mtd/chips/cfi_cmdset_0020.c
@@ -174,11 +174,8 @@ static struct mtd_info *cfi_staa_setup(struct map_info *map)
 
 	mtd = kzalloc_obj(*mtd);
 	//printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
-
-	if (!mtd) {
-		kfree(cfi->cmdset_priv);
-		return NULL;
-	}
+	if (!mtd)
+		goto free_cmdset_priv;
 
 	mtd->priv = map;
 	mtd->type = MTD_NORFLASH;
@@ -187,11 +184,8 @@ static struct mtd_info *cfi_staa_setup(struct map_info *map)
 	mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
 	mtd->eraseregions = kmalloc_objs(struct mtd_erase_region_info,
 					 mtd->numeraseregions);
-	if (!mtd->eraseregions) {
-		kfree(cfi->cmdset_priv);
-		kfree(mtd);
-		return NULL;
-	}
+	if (!mtd->eraseregions)
+		goto free_mtd;
 
 	for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
 		unsigned long ernum, ersize;
@@ -213,9 +207,7 @@ static struct mtd_info *cfi_staa_setup(struct map_info *map)
 		/* Argh */
 		printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
 		kfree(mtd->eraseregions);
-		kfree(cfi->cmdset_priv);
-		kfree(mtd);
-		return NULL;
+		goto free_mtd;
 	}
 
 	for (i=0; i<mtd->numeraseregions;i++){
@@ -242,6 +234,12 @@ static struct mtd_info *cfi_staa_setup(struct map_info *map)
 	__module_get(THIS_MODULE);
 	mtd->name = map->name;
 	return mtd;
+
+free_mtd:
+	kfree(mtd);
+free_cmdset_priv:
+	kfree(cfi->cmdset_priv);
+	return NULL;
 }
 
 
-- 
2.54.0




More information about the linux-mtd mailing list