From f510a9c66db03b86bfca657ec0f73d9056a2a619 Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Tue, 13 Jan 2026 19:44:31 +0900 Subject: [PATCH] nice: -n huge_num true --- src/uu/nice/src/nice.rs | 19 +++++++++++++------ tests/by-util/test_nice.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/uu/nice/src/nice.rs b/src/uu/nice/src/nice.rs index fc1e9057bf9..78036649f1e 100644 --- a/src/uu/nice/src/nice.rs +++ b/src/uu/nice/src/nice.rs @@ -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; @@ -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; @@ -126,12 +129,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } match nstr.parse::() { 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 => { diff --git a/tests/by-util/test_nice.rs b/tests/by-util/test_nice.rs index 73ebf26722f..8dac2527777 100644 --- a/tests/by-util/test_nice.rs +++ b/tests/by-util/test_nice.rs @@ -90,3 +90,33 @@ fn test_trailing_empty_adjustment() { "error: The argument '--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(); + //.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