diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index 477d2d9174d..116783e67e4 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -16,6 +16,7 @@ use std::error::Error; use std::fmt; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Stdout, Write}; +use std::num::IntErrorKind; use std::str::from_utf8; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; @@ -33,6 +34,7 @@ const DEFAULT_TABSTOP: usize = 8; enum ParseError { InvalidCharacter(String), TabSizeCannotBeZero, + TabSizeTooLarge, TabSizesMustBeAscending, } @@ -46,6 +48,7 @@ impl fmt::Display for ParseError { write!(f, "tab size contains invalid character(s): {}", s.quote()) } Self::TabSizeCannotBeZero => write!(f, "tab size cannot be 0"), + Self::TabSizeTooLarge => write!(f, "tab stop value is too large"), Self::TabSizesMustBeAscending => write!(f, "tab sizes must be ascending"), } } @@ -57,12 +60,16 @@ fn tabstops_parse(s: &str) -> Result, ParseError> { let mut nums = Vec::new(); for word in words { - if let Ok(num) = word.parse() { - nums.push(num); - } else { - return Err(ParseError::InvalidCharacter( - word.trim_start_matches(char::is_numeric).to_string(), - )); + match word.parse::() { + Ok(num) => nums.push(num), + Err(e) => match e.kind() { + IntErrorKind::PosOverflow => return Err(ParseError::TabSizeTooLarge), + _ => { + return Err(ParseError::InvalidCharacter( + word.trim_start_matches(char::is_numeric).to_string(), + )) + } + }, } } diff --git a/tests/by-util/test_unexpand.rs b/tests/by-util/test_unexpand.rs index e71e76d5521..c208b624c65 100644 --- a/tests/by-util/test_unexpand.rs +++ b/tests/by-util/test_unexpand.rs @@ -218,3 +218,11 @@ fn test_tabs_with_invalid_chars() { .fails() .stderr_contains("tab size contains invalid character(s): 'x2'"); } + +#[test] +fn test_tabs_shortcut_with_too_large_size() { + let arg = format!("-{}", u128::MAX); + let expected_error = "tab stop value is too large"; + + new_ucmd!().arg(arg).fails().stderr_contains(expected_error); +}