Skip to content

Commit 858f4f0

Browse files
committed
nice: -n huge_num true
1 parent c498595 commit 858f4f0

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/uu/nice/src/nice.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use clap::{Arg, ArgAction, Command};
99
use libc::PRIO_PROCESS;
1010
use std::ffi::OsString;
1111
use std::io::{Error, ErrorKind, Write};
12+
use std::num::IntErrorKind;
1213
use std::os::unix::process::CommandExt;
1314
use std::process;
1415

@@ -115,6 +116,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
115116
format!("getpriority: {}", Error::last_os_error()),
116117
));
117118
}
119+
let nice_bound = 50;
118120

119121
let adjustment = match matches.get_one::<String>(options::ADJUSTMENT) {
120122
Some(nstr) => {
@@ -126,12 +128,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
126128
}
127129
match nstr.parse::<i32>() {
128130
Ok(num) => num,
129-
Err(e) => {
130-
return Err(USimpleError::new(
131-
125,
132-
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
133-
));
134-
}
131+
Err(e) => match e.kind() {
132+
IntErrorKind::PosOverflow => nice_bound,
133+
IntErrorKind::NegOverflow => -nice_bound,
134+
_ => {
135+
return Err(USimpleError::new(
136+
125,
137+
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
138+
));
139+
}
140+
},
135141
}
136142
}
137143
None => {

tests/by-util/test_nice.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,29 @@ 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+
}
110+
111+
#[test]
112+
fn test_sign_middle() {
113+
new_ucmd!().args(&["-n", "-2+4", "true"])
114+
..fails_with_code(125).no_stdout().stderr_contains("invalid");
115+
}
116+
//uu: "-2+4" is not a valid number: invalid digit found in string
117+
//gnu: invalid adjustment `-2+4'
118+
//Both message is fine

0 commit comments

Comments
 (0)