Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/uu/nice/src/nice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use clap::{Arg, ArgAction, Command};
use libc::PRIO_PROCESS;
use std::ffi::OsString;
use std::io::{Error, ErrorKind, Write};
use std::num::IntErrorKind;
use std::os::unix::process::CommandExt;
use std::process;

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

let adjustment = match matches.get_one::<String>(options::ADJUSTMENT) {
Some(nstr) => {
Expand All @@ -126,12 +128,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
match nstr.parse::<i32>() {
Ok(num) => num,
Err(e) => {
return Err(USimpleError::new(
125,
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
));
}
Err(e) => match e.kind() {
IntErrorKind::PosOverflow => nice_bound,
IntErrorKind::NegOverflow => -nice_bound,
_ => {
return Err(USimpleError::new(
125,
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
));
}
},
}
}
None => {
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_nice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,32 @@ fn test_trailing_empty_adjustment() {
"error: The argument '--adjustment <adjustment>' requires a value but none was supplied",
);
}

#[test]
fn test_nice_huge() {
new_ucmd!()
.args(&[
"-n",
"99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
"true",
])
.succeeds()
.no_stdout();
}

#[test]
fn test_nice_huge_negative() {
new_ucmd!().args(&["-n", "-9999999999", "true"]).succeeds(); // permission denied
}

#[test]
fn test_sign_middle() {
new_ucmd!()
.args(&["-n", "-2+4", "true"])
.fails_with_code(125)
.no_stdout()
.stderr_contains("invalid");
}
//uu: "-2+4" is not a valid number: invalid digit found in string
//gnu: invalid adjustment `-2+4'
//Both message is fine
Loading