[PATCH v2 0/2] rust: str: parse_int: match kstrtol() sign and newline handling

From: Younes Akhouayri via B4 Relay

Date: Sun Nov 01 2026 - 10:48:20 EST


The documentation of `ParseInt` says that any string parsed by
`kstrtol()` or `kstrtoul()` is also parsed by `from_str()`. That does
not hold today, and `from_str()` also accepts strings that `kstrtol()`
rejects:

input kstrtol() i32::from_str()
"0x-5" -EINVAL Ok(-5)
"0x+5" -EINVAL Ok(5)
"-+5" -EINVAL Ok(-5)
"+0x5" 5 Err(EINVAL)
"5\n" 5 Err(EINVAL)

Patch 1 accepts a sign only before the radix prefix. Patch 2 accepts a
single trailing newline, as `kstrtol()` does. The `0o` and `0b`
prefixes are Rust extensions and are left as they are.

Nothing in-tree depends on the old behaviour except the `rust_minimal`
sample, so this is meant for rust-next. The `Fixes:` tags record where
the behaviour comes from; I am not asking for a stable backport.

Tested on UML with the Rust KUnit suite, plus a temporary KUnit test
(not part of this series) that compares `i32::from_str()` with
`kstrtoint()`, a thin wrapper around `kstrtoll()`, on 13 inputs; all of
them agree. Also booted the `rust_minimal` sample: `test_parameter=0x-5`
is now rejected and `test_parameter=+0x5` gives 5.

Signed-off-by: Younes Akhouayri <git@xxxxxxxxx>
---
Changes in v2:
- Add a doctest for "-0x+5" to patch 1 (Miguel).
- Refer to kstrtol() instead of kstrtoint() in the commit messages and
the cover letter (Miguel).
- Say in the strip_radix() doc comment which from_str_radix() is meant
and why a sign is rejected there (Miguel).
- Note the rust-next target and that no stable backport is requested.
- Link to v1: https://patch.msgid.link/20261027-b4-parse-int-sign-v1-0-7f386318a4b7@xxxxxxxxx

---
Younes Akhouayri (2):
rust: str: parse_int: only accept a sign before the radix prefix
rust: str: parse_int: accept a trailing newline like kstrtol()

rust/kernel/str/parse_int.rs | 36 ++++++++++++++++++++++++++++--------
1 file changed, 28 insertions(+), 8 deletions(-)
---
base-commit: 7d109178cd644268326cd31702ed46c7ac8bb300
change-id: 20261027-b4-parse-int-sign-36e899fc4f43

Best regards,
--
Younes Akhouayri <git@xxxxxxxxx>