From 7efe33108a8bebada31e962cfb8c0d5a70b07efe Mon Sep 17 00:00:00 2001 From: Coba Weel <122735+cobaweel@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:15:59 -0800 Subject: [PATCH] Fix issue 5576 (regex matching bug in expr) 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. --- src/uu/expr/src/syntax_tree.rs | 3 ++- tests/by-util/test_expr.rs | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 0654f2ac3..2260b2e21 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -498,7 +498,8 @@ fn infix_operator_and(values: &[String]) -> String { fn operator_match(values: &[String]) -> Result { 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]) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index f29752f66..ebc2c832f 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -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]