[PATCH 1/2] ubi: build: replace simple_strtoul with kstrtouint in open_mtd_device()

haoyu.lu hechushiguitu666 at gmail.com
Wed May 6 20:23:07 PDT 2026


From: Haoyu Lu <hechushiguitu666 at gmail.com>

Replace the deprecated simple_strtoul() with kstrtouint() for parsing
the MTD device number string. kstrtouint() provides stricter validation
and better error handling.

In open_mtd_device(), a string that can be parsed as a pure number is
treated as an MTD device index; otherwise it is treated as a device
name. kstrtouint() returns 0 on success and -EINVAL if the string
contains non-numeric characters, which aligns with this logic and
eliminates the need for manual endp checks.

For compatibility with kstrtouint(), mtd_num is changed from int to
unsigned int, consistent with the existing simple_strtoul ->
kstrtouint conversions in mtdoops.c and mtdsuper.c.

The multi-line comment is simplified to a concise single-line comment
since the logic is self-explanatory with kstrtouint().

Signed-off-by: Haoyu Lu <hechushiguitu666 at gmail.com>
---
 drivers/mtd/ubi/build.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 674ad87809df..03ef195dc25c 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -1210,21 +1210,16 @@ static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
 static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
 {
 	struct mtd_info *mtd;
-	int mtd_num;
-	char *endp;
+	unsigned int mtd_num;
 
-	mtd_num = simple_strtoul(mtd_dev, &endp, 0);
-	if (*endp != '\0' || mtd_dev == endp) {
-		/*
-		 * This does not look like an ASCII integer, probably this is
-		 * MTD device name.
-		 */
+	if (kstrtouint(mtd_dev, 0, &mtd_num) != 0) {
+		/* Not a plain number, treat as MTD device name */
 		mtd = get_mtd_device_nm(mtd_dev);
 		if (PTR_ERR(mtd) == -ENODEV)
-			/* Probably this is an MTD character device node path */
 			mtd = open_mtd_by_chdev(mtd_dev);
-	} else
+	} else {
 		mtd = get_mtd_device(NULL, mtd_num);
+	}
 
 	return mtd;
 }
-- 
2.17.1




More information about the linux-mtd mailing list