Skip to content

Commit 4bbe164

Browse files
authored
Merge branch 'main' into core-1
2 parents 4ff3c89 + ca13e33 commit 4bbe164

File tree

3 files changed

+43
-50
lines changed

3 files changed

+43
-50
lines changed

src/common/validation.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,6 @@ fn get_canonical_util_name(util_name: &str) -> &str {
6262
}
6363
}
6464

65-
/// Finds a utility with a prefix (e.g., "uu_test" -> "test")
66-
pub fn find_prefixed_util<'a>(
67-
binary_name: &str,
68-
mut util_keys: impl Iterator<Item = &'a str>,
69-
) -> Option<&'a str> {
70-
util_keys.find(|util| {
71-
binary_name.ends_with(*util)
72-
&& binary_name.len() > util.len() // Ensure there's actually a prefix
73-
&& !binary_name[..binary_name.len() - (*util).len()]
74-
.ends_with(char::is_alphanumeric)
75-
})
76-
}
77-
7865
/// Gets the binary path from command line arguments
7966
/// # Panics
8067
/// Panics if the binary path cannot be determined
@@ -123,35 +110,4 @@ mod tests {
123110
assert_eq!(name(Path::new("")), None);
124111
assert_eq!(name(Path::new("/")), None);
125112
}
126-
127-
#[test]
128-
fn test_find_prefixed_util() {
129-
let utils = ["test", "cat", "ls", "cp"];
130-
131-
// Test exact prefixed matches
132-
assert_eq!(
133-
find_prefixed_util("uu_test", utils.iter().copied()),
134-
Some("test")
135-
);
136-
assert_eq!(
137-
find_prefixed_util("my-cat", utils.iter().copied()),
138-
Some("cat")
139-
);
140-
assert_eq!(
141-
find_prefixed_util("prefix_ls", utils.iter().copied()),
142-
Some("ls")
143-
);
144-
145-
// Test non-alphanumeric separator requirement
146-
assert_eq!(find_prefixed_util("prefixcat", utils.iter().copied()), None); // no separator
147-
assert_eq!(find_prefixed_util("testcat", utils.iter().copied()), None); // no separator
148-
149-
// Test no match
150-
assert_eq!(find_prefixed_util("unknown", utils.iter().copied()), None);
151-
assert_eq!(find_prefixed_util("", utils.iter().copied()), None);
152-
153-
// Test exact util name (should not match as prefixed)
154-
assert_eq!(find_prefixed_util("test", utils.iter().copied()), None);
155-
assert_eq!(find_prefixed_util("cat", utils.iter().copied()), None);
156-
}
157113
}

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

0 commit comments

Comments
 (0)