Skip to content

Commit de1f2bd

Browse files
oech3sylvestre
authored andcommitted
nice: -n huge_num true
1 parent d72d6f7 commit de1f2bd

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/uu/nice/src/nice.rs

Lines changed: 13 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

@@ -23,6 +24,8 @@ pub mod options {
2324
pub static COMMAND: &str = "COMMAND";
2425
}
2526

27+
const NICE_BOUND_NO_OVERFLOW: i32 = 50;
28+
2629
fn is_prefix_of(maybe_prefix: &str, target: &str, min_match: usize) -> bool {
2730
if maybe_prefix.len() < min_match || maybe_prefix.len() > target.len() {
2831
return false;
@@ -126,12 +129,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
126129
}
127130
match nstr.parse::<i32>() {
128131
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-
}
132+
Err(e) => match e.kind() {
133+
IntErrorKind::PosOverflow => NICE_BOUND_NO_OVERFLOW,
134+
IntErrorKind::NegOverflow => -NICE_BOUND_NO_OVERFLOW,
135+
_ => {
136+
return Err(USimpleError::new(
137+
125,
138+
translate!("nice-error-invalid-number", "value" => nstr.clone(), "error" => e),
139+
));
140+
}
141+
},
135142
}
136143
}
137144
None => {

tests/by-util/test_nice.rs

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

0 commit comments

Comments
 (0)