Skip to content

Commit

Permalink
Fix issue 5576 (regex matching bug in expr)
Browse files Browse the repository at this point in the history
Issue 5576 reported a bug in expr, found by the fuzzer. The problem
turns out to be with the regex match operator `:`, which is defined in
POSIX and the GNU manual to match the pattern only when it occurs at
the beginning of the string, i.e., the regex has an implicit `^`
prepended to it. We hadn't been doing that.
  • Loading branch information
cobaweel authored and cakebaker committed Nov 23, 2023
1 parent fff1302 commit 7efe331
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ fn infix_operator_and(values: &[String]) -> String {

fn operator_match(values: &[String]) -> Result<String, String> {
assert!(values.len() == 2);
let re = Regex::with_options(&values[1], RegexOptions::REGEX_OPTION_NONE, Syntax::grep())
let re_string = format!("^{}", &values[1]);
let re = Regex::with_options(&re_string, RegexOptions::REGEX_OPTION_NONE, Syntax::grep())
.map_err(|err| err.description().to_string())?;
Ok(if re.captures_len() > 0 {
re.captures(&values[0])
Expand Down
4 changes: 4 additions & 0 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ fn test_regex() {
.args(&["-5", ":", "-\\{0,1\\}[0-9]*$"])
.succeeds()
.stdout_only("2\n");
new_ucmd!()
.args(&["abc", ":", "bc"])
.fails()
.stdout_only("0\n");
}

#[test]
Expand Down

0 comments on commit 7efe331

Please sign in to comment.