Skip to content

Commit 4946922

Browse files
committed
expr: Fix error message for large numbers as range index
1 parent 74ad163 commit 4946922

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/uu/expr/src/syntax_tree.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,15 @@ where
307307
let matched = captures.at(0).unwrap_or_default();
308308
match matched.split_once(',') {
309309
Some(("", "")) => Ok(()),
310-
Some((x, "")) | Some(("", x)) => match x.parse::<i32>() {
311-
Ok(x) if x <= i16::MAX.into() => Ok(()),
312-
Ok(_) => Err(ExprError::TooBigRangeQuantifierIndex),
313-
Err(_) => Err(ExprError::InvalidBracketContent),
314-
},
315-
Some((f, l)) => match (f.parse::<i32>(), l.parse::<i32>()) {
310+
Some((x, "") | ("", x)) if x.parse::<i16>().is_ok() => Ok(()),
311+
Some((_, "") | ("", _)) => Err(ExprError::TooBigRangeQuantifierIndex),
312+
Some((f, l)) => match (f.parse::<i16>(), l.parse::<i16>()) {
316313
(Ok(f), Ok(l)) if f > l => Err(ExprError::InvalidBracketContent),
317-
(Ok(f), Ok(l)) if f > i16::MAX.into() || l > i16::MAX.into() => {
318-
Err(ExprError::TooBigRangeQuantifierIndex)
319-
}
320314
(Ok(_), Ok(_)) => Ok(()),
321-
_ => Err(ExprError::InvalidBracketContent),
322-
},
323-
None => match matched.parse::<i32>() {
324-
Ok(x) if x <= i16::MAX.into() => Ok(()),
325-
Ok(_) => Err(ExprError::TooBigRangeQuantifierIndex),
326-
Err(_) => Err(ExprError::InvalidBracketContent),
315+
_ => Err(ExprError::TooBigRangeQuantifierIndex),
327316
},
317+
None if matched.parse::<i16>().is_ok() => Ok(()),
318+
None => Err(ExprError::TooBigRangeQuantifierIndex),
328319
}
329320
} else {
330321
Err(ExprError::InvalidBracketContent)

tests/by-util/test_expr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,26 @@ fn test_regex_range_quantifier() {
472472
.args(&["ab", ":", "ab\\{\\}"])
473473
.fails()
474474
.stderr_only("expr: Invalid content of \\{\\}\n");
475+
new_ucmd!()
476+
.args(&["_", ":", "a\\{12345678901234567890\\}"])
477+
.fails()
478+
.stderr_only("expr: Regular expression too big\n");
479+
new_ucmd!()
480+
.args(&["_", ":", "a\\{12345678901234567890,\\}"])
481+
.fails()
482+
.stderr_only("expr: Regular expression too big\n");
483+
new_ucmd!()
484+
.args(&["_", ":", "a\\{,12345678901234567890\\}"])
485+
.fails()
486+
.stderr_only("expr: Regular expression too big\n");
487+
new_ucmd!()
488+
.args(&["_", ":", "a\\{1,12345678901234567890\\}"])
489+
.fails()
490+
.stderr_only("expr: Regular expression too big\n");
491+
new_ucmd!()
492+
.args(&["_", ":", "a\\{1,1234567890abcdef\\}"])
493+
.fails()
494+
.stderr_only("expr: Invalid content of \\{\\}\n");
475495
}
476496

477497
#[test]

0 commit comments

Comments
 (0)