1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

unexpand: handle too large "tabs" arguments

This commit is contained in:
Daniel Hofstetter 2022-06-12 16:07:23 +02:00 committed by Sylvestre Ledru
parent 75edeea5e4
commit cf605c24d0
2 changed files with 21 additions and 6 deletions

View file

@ -16,6 +16,7 @@ use std::error::Error;
use std::fmt; use std::fmt;
use std::fs::File; use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Stdout, Write}; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Stdout, Write};
use std::num::IntErrorKind;
use std::str::from_utf8; use std::str::from_utf8;
use unicode_width::UnicodeWidthChar; use unicode_width::UnicodeWidthChar;
use uucore::display::Quotable; use uucore::display::Quotable;
@ -33,6 +34,7 @@ const DEFAULT_TABSTOP: usize = 8;
enum ParseError { enum ParseError {
InvalidCharacter(String), InvalidCharacter(String),
TabSizeCannotBeZero, TabSizeCannotBeZero,
TabSizeTooLarge,
TabSizesMustBeAscending, TabSizesMustBeAscending,
} }
@ -46,6 +48,7 @@ impl fmt::Display for ParseError {
write!(f, "tab size contains invalid character(s): {}", s.quote()) write!(f, "tab size contains invalid character(s): {}", s.quote())
} }
Self::TabSizeCannotBeZero => write!(f, "tab size cannot be 0"), 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"), Self::TabSizesMustBeAscending => write!(f, "tab sizes must be ascending"),
} }
} }
@ -57,12 +60,16 @@ fn tabstops_parse(s: &str) -> Result<Vec<usize>, ParseError> {
let mut nums = Vec::new(); let mut nums = Vec::new();
for word in words { for word in words {
if let Ok(num) = word.parse() { match word.parse::<usize>() {
nums.push(num); Ok(num) => nums.push(num),
} else { Err(e) => match e.kind() {
IntErrorKind::PosOverflow => return Err(ParseError::TabSizeTooLarge),
_ => {
return Err(ParseError::InvalidCharacter( return Err(ParseError::InvalidCharacter(
word.trim_start_matches(char::is_numeric).to_string(), word.trim_start_matches(char::is_numeric).to_string(),
)); ))
}
},
} }
} }

View file

@ -218,3 +218,11 @@ fn test_tabs_with_invalid_chars() {
.fails() .fails()
.stderr_contains("tab size contains invalid character(s): 'x2'"); .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);
}