The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "haoyu.lu" <hechushiguitu666@gmail.com>
To: Miquel Raynal <miquel.raynal@bootlin.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>
Cc: Zhihao Cheng <chengzhihao1@huawei.com>,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	Haoyu Lu <hechushiguitu666@gmail.com>
Subject: [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()
Date: Thu,  7 May 2026 03:23:08 +0000	[thread overview]
Message-ID: <20260507032308.16485-2-hechushiguitu666@gmail.com> (raw)
In-Reply-To: <20260507032308.16485-1-hechushiguitu666@gmail.com>

From: Haoyu Lu <hechushiguitu666@gmail.com>

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.

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

diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 03ef195dc25c..7cb6ba4a3840 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -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;
-- 
2.17.1


  reply	other threads:[~2026-05-07  3:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` haoyu.lu [this message]
2026-05-07  7:30   ` [PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int() 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260507032308.16485-2-hechushiguitu666@gmail.com \
    --to=hechushiguitu666@gmail.com \
    --cc=chengzhihao1@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=vigneshr@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox