1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

expr: Fix error message for large numbers as range index

This commit is contained in:
Teemu Pätsi 2025-05-27 16:45:54 +03:00
parent 74ad163da9
commit 4946922c0f
No known key found for this signature in database
2 changed files with 26 additions and 15 deletions

View file

@ -307,24 +307,15 @@ where
let matched = captures.at(0).unwrap_or_default();
match matched.split_once(',') {
Some(("", "")) => Ok(()),
Some((x, "")) | Some(("", x)) => match x.parse::<i32>() {
Ok(x) if x <= i16::MAX.into() => Ok(()),
Ok(_) => Err(ExprError::TooBigRangeQuantifierIndex),
Err(_) => Err(ExprError::InvalidBracketContent),
},
Some((f, l)) => match (f.parse::<i32>(), l.parse::<i32>()) {
Some((x, "") | ("", x)) if x.parse::<i16>().is_ok() => Ok(()),
Some((_, "") | ("", _)) => Err(ExprError::TooBigRangeQuantifierIndex),
Some((f, l)) => match (f.parse::<i16>(), l.parse::<i16>()) {
(Ok(f), Ok(l)) if f > l => Err(ExprError::InvalidBracketContent),
(Ok(f), Ok(l)) if f > i16::MAX.into() || l > i16::MAX.into() => {
Err(ExprError::TooBigRangeQuantifierIndex)
}
(Ok(_), Ok(_)) => Ok(()),
_ => Err(ExprError::InvalidBracketContent),
},
None => match matched.parse::<i32>() {
Ok(x) if x <= i16::MAX.into() => Ok(()),
Ok(_) => Err(ExprError::TooBigRangeQuantifierIndex),
Err(_) => Err(ExprError::InvalidBracketContent),
_ => Err(ExprError::TooBigRangeQuantifierIndex),
},
None if matched.parse::<i16>().is_ok() => Ok(()),
None => Err(ExprError::TooBigRangeQuantifierIndex),
}
} else {
Err(ExprError::InvalidBracketContent)

View file

@ -472,6 +472,26 @@ fn test_regex_range_quantifier() {
.args(&["ab", ":", "ab\\{\\}"])
.fails()
.stderr_only("expr: Invalid content of \\{\\}\n");
new_ucmd!()
.args(&["_", ":", "a\\{12345678901234567890\\}"])
.fails()
.stderr_only("expr: Regular expression too big\n");
new_ucmd!()
.args(&["_", ":", "a\\{12345678901234567890,\\}"])
.fails()
.stderr_only("expr: Regular expression too big\n");
new_ucmd!()
.args(&["_", ":", "a\\{,12345678901234567890\\}"])
.fails()
.stderr_only("expr: Regular expression too big\n");
new_ucmd!()
.args(&["_", ":", "a\\{1,12345678901234567890\\}"])
.fails()
.stderr_only("expr: Regular expression too big\n");
new_ucmd!()
.args(&["_", ":", "a\\{1,1234567890abcdef\\}"])
.fails()
.stderr_only("expr: Invalid content of \\{\\}\n");
}
#[test]