Skip to content

Commit 7a75ddd

Browse files
committed
nice: -n huge_num true
1 parent 7fe4513 commit 7a75ddd

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/uu/nice/src/nice.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
127127
match nstr.parse::<i32>() {
128128
Ok(num) => num,
129129
Err(e) => {
130-
return Err(USimpleError::new(
131-
125,
132-
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
133-
));
130+
// clamp huge values
131+
let trimmed = nstr.trim();
132+
let is_num = trimmed
133+
.chars()
134+
.all(|c| c.is_ascii_digit() || c == '-' || c == '+');
135+
if is_num {
136+
if trimmed.starts_with('-') { -30 } else { 30 }
137+
} else {
138+
return Err(USimpleError::new(
139+
125,
140+
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
141+
));
142+
}
134143
}
135144
}
136145
}

tests/by-util/test_nice.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,15 @@ fn test_trailing_empty_adjustment() {
9090
"error: The argument '--adjustment <adjustment>' requires a value but none was supplied",
9191
);
9292
}
93+
94+
#[test]
95+
fn test_nice_huge() {
96+
new_ucmd!()
97+
.args(&[
98+
"-n",
99+
"99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
100+
"true",
101+
])
102+
.succeeds()
103+
.no_stdout();
104+
}

0 commit comments

Comments
 (0)