1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #3655 from cakebaker/show_tab_size_too_large_error

unexpand: handle too large "tabs" arguments
This commit is contained in:
Sylvestre Ledru 2022-06-22 15:26:37 +02:00 committed by GitHub
commit 1e7903590f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View file

@ -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<Vec<usize>, 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::<usize>() {
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(),
))
}
},
}
}

View file

@ -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);
}