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
19 changes: 13 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 All @@ -23,6 +24,8 @@ pub mod options {
pub static COMMAND: &str = "COMMAND";
}

const NICE_BOUND_NO_OVERFLOW: i32 = 50;

fn is_prefix_of(maybe_prefix: &str, target: &str, min_match: usize) -> bool {
if maybe_prefix.len() < min_match || maybe_prefix.len() > target.len() {
return false;
Expand Down Expand Up @@ -126,12 +129,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_NO_OVERFLOW,
IntErrorKind::NegOverflow => -NICE_BOUND_NO_OVERFLOW,
_ => {
return Err(USimpleError::new(
125,
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
));
}
},
}
}
None => {
Expand Down
30 changes: 30 additions & 0 deletions tests/by-util/test_nice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,33 @@ 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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only positive overflow is tested. Should add a test for negative overflow (e.g., -999...9) to ensure both clamping directions work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Negative is root test, would not work on the CI.
...
Oh yes. It should work with permission denied.

"99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
"true",
])
.succeeds()
.no_stdout();
}

#[test]
fn test_nice_huge_negative() {
new_ucmd!().args(&["-n", "-9999999999", "true"]).succeeds();
//.stderr_contains("Permission denied"); Depending on platform?
}

#[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