Skip to content

Commit d7cd76f

Browse files
committed
nice: -n huge_num true
1 parent f2bec7b commit d7cd76f

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/uu/nice/src/nice.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,23 @@ 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_valid_number = trimmed
133+
.chars()
134+
.all(|c| c.is_ascii_digit() || c == '-' || c == '+');
135+
if is_valid_number {
136+
if trimmed.starts_with('-') {
137+
i32::MIN
138+
} else {
139+
i32::MAX
140+
}
141+
} else {
142+
return Err(USimpleError::new(
143+
125,
144+
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
145+
));
146+
}
134147
}
135148
}
136149
}

tests/by-util/test_nice.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,20 @@ 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+
}
105+
106+
#[test]
107+
fn test_nice_huge_negative() {
108+
new_ucmd!().args(&["-n", "-9999999999", "true"]).succeeds(); // permission denied
109+
}

0 commit comments

Comments
 (0)