[PATCH 0/2] rust: str: parse_int: match kstrtol() sign and newline handling
From: Younes Akhouayri via B4 Relay
Date: Tue Oct 27 2026 - 08:18:55 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 kstrtoint() 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.
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()` on 12 inputs; all of them now 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>
---
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 | 33 +++++++++++++++++++++++++--------
1 file changed, 25 insertions(+), 8 deletions(-)
---
base-commit: 7d109178cd644268326cd31702ed46c7ac8bb300
change-id: 20261027-b4-parse-int-sign-36e899fc4f43
Best regards,
--
Younes Akhouayri <git@xxxxxxxxx>