Skip to content

Commit 8c2cd78

Browse files
hoshinolinajannau
authored andcommitted
rust: module_param: Tolerate a trailing newline when parsing
This is the same behavior as kstrtol/kstrtoul, and allows simple `echo 0 > /sys/module/foo/parameters/bar` commands to work. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 88cfb15 commit 8c2cd78

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

rust/kernel/module_param.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ pub trait ModuleParam: core::fmt::Display + core::marker::Sized {
122122
/// Strings beginning with `0x`, `0o`, or `0b` are parsed as hex, octal, or
123123
/// binary respectively. Strings beginning with `0` otherwise are parsed as
124124
/// octal. Anything else is parsed as decimal. A leading `+` or `-` is also
125-
/// permitted. Any string parsed by [`kstrtol()`] or [`kstrtoul()`] will be
126-
/// successfully parsed.
125+
/// permitted. The string may contain a trailing newline. Any string parsed
126+
/// by [`kstrtol()`] or [`kstrtoul()`] will be successfully parsed.
127127
///
128128
/// [`kstrtol()`]: https://www.kernel.org/doc/html/latest/core-api/kernel-api.html#c.kstrtol
129129
/// [`kstrtoul()`]: https://www.kernel.org/doc/html/latest/core-api/kernel-api.html#c.kstrtoul
@@ -132,6 +132,7 @@ trait ParseInt: Sized {
132132
fn checked_neg(self) -> Option<Self>;
133133

134134
fn from_str_unsigned(src: &str) -> Result<Self, core::num::ParseIntError> {
135+
let src = src.strip_suffix('\n').unwrap_or(src);
135136
let (radix, digits) = if let Some(n) = src.strip_prefix("0x") {
136137
(16, n)
137138
} else if let Some(n) = src.strip_prefix("0X") {

0 commit comments

Comments
 (0)