Skip to content

Commit 120ab2e

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

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/uu/expr/src/syntax_tree.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,15 @@ where
303303

304304
// Check if parsed quantifier is valid
305305
let re = Regex::new(r"^(\d*,\d*|\d+)$").expect("valid regular expression");
306+
let is_natural_number = |string: &str| string.chars().all(|x| x.is_ascii_digit());
306307
if let Some(captures) = re.captures(&quantifier) {
307308
let matched = captures.at(0).unwrap_or_default();
308309
match matched.split_once(',') {
309310
Some(("", "")) => Ok(()),
310311
Some((x, "")) | Some(("", x)) => match x.parse::<i32>() {
311312
Ok(x) if x <= i16::MAX.into() => Ok(()),
312313
Ok(_) => Err(ExprError::TooBigRangeQuantifierIndex),
314+
Err(_) if is_natural_number(x) => Err(ExprError::TooBigRangeQuantifierIndex),
313315
Err(_) => Err(ExprError::InvalidBracketContent),
314316
},
315317
Some((f, l)) => match (f.parse::<i32>(), l.parse::<i32>()) {
@@ -318,11 +320,13 @@ where
318320
Err(ExprError::TooBigRangeQuantifierIndex)
319321
}
320322
(Ok(_), Ok(_)) => Ok(()),
323+
_ if is_natural_number(l) => Err(ExprError::TooBigRangeQuantifierIndex),
321324
_ => Err(ExprError::InvalidBracketContent),
322325
},
323326
None => match matched.parse::<i32>() {
324327
Ok(x) if x <= i16::MAX.into() => Ok(()),
325328
Ok(_) => Err(ExprError::TooBigRangeQuantifierIndex),
329+
Err(_) if is_natural_number(matched) => Err(ExprError::TooBigRangeQuantifierIndex),
326330
Err(_) => Err(ExprError::InvalidBracketContent),
327331
},
328332
}

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)