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

expand: handle too large tab size

This commit is contained in:
Daniel Hofstetter 2022-06-05 13:49:11 +02:00
parent 69c2871336
commit c14ff14e99
2 changed files with 41 additions and 23 deletions

View file

@ -17,6 +17,7 @@ use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
use std::num::IntErrorKind;
use std::str::from_utf8;
use unicode_width::UnicodeWidthChar;
use uucore::display::Quotable;
@ -65,6 +66,7 @@ enum ParseError {
InvalidCharacter(String),
SpecifierNotAtStartOfNumber(String, String),
TabSizeCannotBeZero,
TabSizeTooLarge(String),
TabSizesMustBeAscending,
}
@ -84,6 +86,7 @@ impl fmt::Display for ParseError {
s.quote(),
),
Self::TabSizeCannotBeZero => write!(f, "tab size cannot be 0"),
Self::TabSizeTooLarge(s) => write!(f, "tab stop is too large {}", s.quote()),
Self::TabSizesMustBeAscending => write!(f, "tab sizes must be ascending"),
}
}
@ -122,7 +125,8 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
_ => {
// Parse a number from the byte sequence.
let s = from_utf8(&bytes[i..]).unwrap();
if let Ok(num) = s.parse::<usize>() {
match s.parse::<usize>() {
Ok(num) => {
// Tab size must be positive.
if num == 0 {
return Err(ParseError::TabSizeCannotBeZero);
@ -138,7 +142,12 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
// Append this tab stop to the list of all tabstops.
nums.push(num);
break;
} else {
}
Err(e) => {
if *e.kind() == IntErrorKind::PosOverflow {
return Err(ParseError::TabSizeTooLarge(s.to_string()));
}
let s = s.trim_start_matches(char::is_numeric);
if s.starts_with('/') || s.starts_with('+') {
return Err(ParseError::SpecifierNotAtStartOfNumber(
@ -153,6 +162,7 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
}
}
}
}
// If no numbers could be parsed (for example, if `s` were "+,+,+"),
// then just use the default tabstops.
if nums.is_empty() {

View file

@ -237,3 +237,11 @@ fn test_tabs_with_invalid_chars() {
.fails()
.stderr_contains("tab size contains invalid character(s): 'x2'");
}
#[test]
fn test_tabs_with_too_large_size() {
let arg = format!("--tabs={}", u128::MAX);
let expected_error = format!("tab stop is too large '{}'", u128::MAX);
new_ucmd!().arg(arg).fails().stderr_contains(expected_error);
}