The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 1/2] ubi: build: replace simple_strtoul with kstrtouint in open_mtd_device()
@ 2026-05-07  3:23 haoyu.lu
  2026-05-07  3:23 ` [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int() haoyu.lu
  2026-05-07  7:40 ` [PATCH 1/2] ubi: build: replace simple_strtoul with kstrtouint in open_mtd_device() Zhihao Cheng
  0 siblings, 2 replies; 7+ messages in thread
From: haoyu.lu @ 2026-05-07  3:23 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra
  Cc: Zhihao Cheng, linux-mtd, linux-kernel, Haoyu Lu

From: Haoyu Lu <hechushiguitu666@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@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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-05-07  8:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-07  3:23 [PATCH 1/2] ubi: build: replace simple_strtoul with kstrtouint in open_mtd_device() haoyu.lu
2026-05-07  3:23 ` [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int() haoyu.lu
2026-05-07  7:30   ` Richard Weinberger
2026-05-07  7:45   ` Zhihao Cheng
2026-05-07  7:50     ` Richard Weinberger
2026-05-07  8:25       ` Haoyu Lu
2026-05-07  7:40 ` [PATCH 1/2] ubi: build: replace simple_strtoul with kstrtouint in open_mtd_device() Zhihao Cheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox