Repository navigation
Description
Known: #48 (merged PR) — #48; #23 (closed: fixed by #48) — #23
Follow-up to #23 (fixed by #48), which only handled a repetition operator at the very first
character of the pattern. In -E mode GNU grep accepts *, +, ? or {m,n} with nothing
to repeat anywhere an expression starts (after ( or | too), warns
<op> at start of expression, treats the operator as matching the empty string, and searches.
uu_grep still exits 2 with an Oniguruma error for a|*b, (*a), x|+a, and for a doubled
leading operator such as **a (the error then names *a, a pattern the user never typed,
because the first * was already stripped).
Related: #23, PR #48 (leading operator only). PR #150 (unmatched )) is a different case.
Reproduction
Rust (incorrect)
$ printf '*a\na\nb\n' | ./target/release/grep -E 'b|*a'
grep: invalid pattern "b|*a": target of repeat operator is not specified
# Exit code: 2
$ printf '*a\na\nb\n' | ./target/release/grep -E '(*a)'
grep: invalid pattern "(*a)": target of repeat operator is not specified
# Exit code: 2GNU (correct)
$ printf '*a\na\nb\n' | LC_ALL=C /usr/bin/grep -E 'b|*a'
grep: warning: * at start of expression
*a
a
b
# Exit code: 0
$ printf '*a\na\nb\n' | LC_ALL=C /usr/bin/grep -E '(*a)'
grep: warning: * at start of expression
*a
a
# Exit code: 0-G, -F and -P are not affected.
Root cause
src/matcher.rs:292, in CompiledPattern::compile:
if let Some((op, rest)) = strip_leading_repeat_operator(pattern) {
show_warning!("{op} at start of expression");
normalized_pattern = Some(rest.to_string());
}Only one operator at byte 0 is handled. Everything else reaches Oniguruma's
Syntax::gnu_regex(), whose behaviour includes ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS, so any
operator with no target (after (, | or another operator) is a hard error.
Test
#[test]
fn ere_repeat_operator_after_group_or_alternation_matches_empty() {
let cases = [("q|*z", "*z\nz\nq\nw\n", "*z\nz\nq\n"), ("(+z)", "*z\nz\nw\n", "*z\nz\n")];
for (pattern, input, expected) in cases {
let (_s, mut c) = ucmd();
c.args(&["-E", pattern])
.pipe_in(input)
.succeeds()
.stdout_is(expected)
.stderr_contains("at start of expression");
}
}Reported by Anthropic's OSS Scanner (ANT-2026-JB8ODDBQ).
Activity
grep: allow repeat operators with nothing to repeat after ( or |