Skip to contentSkip to conversation

ubi: build: replace simple_strtoul with kstrtouint in open_mtd_device()

PATCH In review haoyu.lu 3 participants 7 messages May 7

Subsystems (2)
lore
Share & tools
lore

haoyu.lu

author
#0 lore raw

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

From: Haoyu Lu <[email protected]>

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().

---
 drivers/mtd/ubi/build.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/mtd/ubi/build.c +5 -10
index 674ad87809df..03ef195dc25c 100644
@@ -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;
 }
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

From: Haoyu Lu <[email protected]>

Replace the deprecated simple_strtoul() with kstrtoul() in the bytes_str_to_int() helper function. Since kstrtoul() rejects trailing non-numeric characters (such as the G/M/K suffixes), the numeric prefix is first extracted with strspn() and then parsed separately before handling the suffix.

This provides proper error handling through the kstrto* family while preserving the existing suffix semantics for byte count parameters.

Note: the original simple_strtoul() with base=0 accepted hexadecimal (0x prefix) and octal (0 prefix) formats, while kstrtoul() with base=10 only supports decimal. This is not a practical concern since MTD byte count parameters are always specified as decimal values in boot parameters.

---
 drivers/mtd/ubi/build.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/mtd/ubi/build.c +15 -4
index 03ef195dc25c..7cb6ba4a3840 100644
@@ -1426,16 +1426,27 @@ module_exit(ubi_exit);
  */
 static int bytes_str_to_int(const char *str)
 {
-	char *endp;
 	unsigned long result;
+	unsigned int num_len;
+	char num_buf[32];
 
-	result = simple_strtoul(str, &endp, 0);
-	if (str == endp || result >= INT_MAX) {
+	/* Find the length of the numeric prefix */
+	num_len = strspn(str, "0123456789");
+	if (num_len == 0 || num_len >= sizeof(num_buf)) {
 		pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
 		return -EINVAL;
 	}
 
-	switch (*endp) {
+	/* Parse the numeric part */
+	memcpy(num_buf, str, num_len);
+	num_buf[num_len] = '\0';
+	if (kstrtoul(num_buf, 10, &result) < 0 || result >= INT_MAX) {
+		pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
+		return -EINVAL;
+	}
+
+	/* Handle suffix */
+	switch (str[num_len]) {
 	case 'G':
 		result *= 1024;
 		fallthrough;
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

----- Ursprüngliche Mail -----

Quoted 25 lines

Von: "haoyu.lu" <[email protected]>
An: "Miquel Raynal" <[email protected]>, "richard" <[email protected]>, "Vignesh Raghavendra" <[email protected]>
CC: "chengzhihao1" <[email protected]>, "linux-mtd" <[email protected]>, "linux-kernel" <[email protected]>, "Haoyu Lu" <[email protected]>
Gesendet: Donnerstag, 7. Mai 2026 05:23:08
Betreff: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

From: Haoyu Lu <[email protected]>

Replace the deprecated simple_strtoul() with kstrtoul() in the

bytes_str_to_int() helper function. Since kstrtoul() rejects trailing non-numeric characters (such as the G/M/K suffixes), the numeric prefix is first extracted with strspn() and then parsed separately before handling the suffix.

This provides proper error handling through the kstrto* family while preserving the existing suffix semantics for byte count parameters.

Note: the original simple_strtoul() with base=0 accepted hexadecimal (0x prefix) and octal (0 prefix) formats, while kstrtoul() with base=10 only supports decimal. This is not a practical concern since MTD byte count parameters are always specified as decimal values in boot parameters.

Quoted 4 lines

---
drivers/mtd/ubi/build.c | 19 +++++++++++++++----

1 file changed, 15 insertions(+), 4 deletions(-)

Your patch does not explain why this change is needed.
From what I can tell, it just makes the existing code more complex and fixes no real issues.

Is simple_strtoul() really deprecated? If so, where? AFAICT, the documentation just mentions:
"This function has caveats. Please use kstrtol instead."

Do these these caveats hurt the current code?

Thanks,
//richard

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

reviewer
#3 lore raw

Re: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

In reply to haoyu.lu
Quoted 8 lines · haoyu.lu

From: Haoyu Lu <[email protected]>

Replace the deprecated simple_strtoul() with kstrtoul() in the bytes_str_to_int() helper function. Since kstrtoul() rejects trailing non-numeric characters (such as the G/M/K suffixes), the numeric prefix is first extracted with strspn() and then parsed separately before handling the suffix.

Quoted 8 lines

This provides proper error handling through the kstrto* family while preserving the existing suffix semantics for byte count parameters.

Note: the original simple_strtoul() with base=0 accepted hexadecimal (0x prefix) and octal (0 prefix) formats, while kstrtoul() with base=10 only supports decimal. This is not a practical concern since MTD byte count parameters are always specified as decimal values in boot parameters.

The ubi could be loaded by module insertion, many vendors set param 'vid_hdr_offs' to save space. We cannot make the assumption that all users use the decimal system.

Quoted 43 lines
---
  drivers/mtd/ubi/build.c | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)
drivers/mtd/ubi/build.c +15 -4
index 03ef195dc25c..7cb6ba4a3840 100644
@@ -1426,16 +1426,27 @@ module_exit(ubi_exit);
   */
  static int bytes_str_to_int(const char *str)
  {
-	char *endp;
  	unsigned long result;
+	unsigned int num_len;
+	char num_buf[32];
 
-	result = simple_strtoul(str, &endp, 0);
-	if (str == endp || result >= INT_MAX) {
+	/* Find the length of the numeric prefix */
+	num_len = strspn(str, "0123456789");
+	if (num_len == 0 || num_len >= sizeof(num_buf)) {
  		pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
  		return -EINVAL;
  	}
 
-	switch (*endp) {
+	/* Parse the numeric part */
+	memcpy(num_buf, str, num_len);
+	num_buf[num_len] = '\0';
+	if (kstrtoul(num_buf, 10, &result) < 0 || result >= INT_MAX) {
+		pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
+		return -EINVAL;
+	}
+
+	/* Handle suffix */
+	switch (str[num_len]) {
  	case 'G':
  		result *= 1024;
  		fallthrough;
#4 lore raw

Re: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

----- Ursprüngliche Mail -----

Quoted 19 lines

Von: "chengzhihao1" <[email protected]>
An: "haoyu.lu" <[email protected]>, "Miquel Raynal" <[email protected]>, "richard" <[email protected]>, "Vignesh Raghavendra" <[email protected]>
CC: "linux-mtd" <[email protected]>, "linux-kernel" <[email protected]>
Gesendet: Donnerstag, 7. Mai 2026 09:45:24
Betreff: Re: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

Quoted 11 lines · haoyu.lu

From: Haoyu Lu <[email protected]>

Replace the deprecated simple_strtoul() with kstrtoul() in the bytes_str_to_int() helper function. Since kstrtoul() rejects trailing non-numeric characters (such as the G/M/K suffixes), the numeric prefix is first extracted with strspn() and then parsed separately before handling the suffix.

This provides proper error handling through the kstrto* family while preserving the existing suffix semantics for byte count parameters.

Quoted 8 lines
Quoted 5 lines

Note: the original simple_strtoul() with base=0 accepted hexadecimal (0x prefix) and octal (0 prefix) formats, while kstrtoul() with base=10 only supports decimal. This is not a practical concern since MTD byte count parameters are always specified as decimal values in boot parameters.

The ubi could be loaded by module insertion, many vendors set param 'vid_hdr_offs' to save space. We cannot make the assumption that all users use the decimal system.

That's a good point!

In general I'm not fond of such changes. They change code for the sake of changing code without fixing actual issues.
I'll happily accept patches that point out real issues with the current usage of various string functions, though.

Thanks,
//richard

author
#5 lore raw

Re: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

Hi Richard, Zhihao,

Thanks for the feedback. I will drop this patch.

Thanks,
Haoyu

Quoted 39 lines · Richard Weinberger

----- Ursprüngliche Mail -----

Quoted 19 lines

Von: "chengzhihao1" <[email protected]>
An: "haoyu.lu" <[email protected]>, "Miquel Raynal" <[email protected]>, "richard" <[email protected]>, "Vignesh Raghavendra" <[email protected]>
CC: "linux-mtd" <[email protected]>, "linux-kernel" <[email protected]>
Gesendet: Donnerstag, 7. Mai 2026 09:45:24
Betreff: Re: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()

Quoted 11 lines · haoyu.lu

From: Haoyu Lu <[email protected]>

Replace the deprecated simple_strtoul() with kstrtoul() in the bytes_str_to_int() helper function. Since kstrtoul() rejects trailing non-numeric characters (such as the G/M/K suffixes), the numeric prefix is first extracted with strspn() and then parsed separately before handling the suffix.

This provides proper error handling through the kstrto* family while preserving the existing suffix semantics for byte count parameters.

Quoted 8 lines
Quoted 5 lines

Note: the original simple_strtoul() with base=0 accepted hexadecimal (0x prefix) and octal (0 prefix) formats, while kstrtoul() with base=10 only supports decimal. This is not a practical concern since MTD byte count parameters are always specified as decimal values in boot parameters.

The ubi could be loaded by module insertion, many vendors set param 'vid_hdr_offs' to save space. We cannot make the assumption that all users use the decimal system.

That's a good point!

In general I'm not fond of such changes. They change code for the sake of changing code without fixing actual issues.
I'll happily accept patches that point out real issues with the current usage of various string functions, though.

Thanks,
//richard

reviewer Reviewed-by on v1 1/2
#6 lore raw
In reply to haoyu.lu
Quoted 16 lines · haoyu.lu

From: Haoyu Lu <[email protected]>

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.

Quoted 8 lines

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

---
  drivers/mtd/ubi/build.c | 15 +++++----------
  1 file changed, 5 insertions(+), 10 deletions(-)
Quoted 32 lines
drivers/mtd/ubi/build.c +5 -10
index 674ad87809df..03ef195dc25c 100644
@@ -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;
  }