[PATCH 2/2] ubi: build: replace simple_strtoul with kstrtoul in bytes_str_to_int()
Zhihao Cheng
chengzhihao1 at huawei.com
Thu May 7 00:45:24 PDT 2026
在 2026/5/7 11:23, haoyu.lu 写道:
> From: Haoyu Lu <hechushiguitu666 at 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.
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.
>
> Signed-off-by: Haoyu Lu <hechushiguitu666 at 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;
>
More information about the linux-mtd
mailing list