Repository navigation
Open
Labels
bugSomething isn't workinggnu-compatBehaviour differs from GNUgood first issueGood for newcomersoss-scannerFound by Anthropic's OSS Scanner
Description
-P uses Oniguruma's Perl_NG syntax, and a few of its settings differ from PCRE2 (and one is
lost by our own code). As a result uu_grep -P rejects or silently misreads patterns that GNU
grep -P accepts:
a(?i)b|c: the inline(?i)does not carry over to thecbranch, soCis not matched.(?<=Host: |host=)x: look-behind alternatives of different lengths are rejected.(?<w>\w+) \1: a numbered back-reference in a pattern that also has a named group is rejected.\dmatches non-ASCII digits such as U+0660; in GNU-P,\dis[0-9].
This issue is only about these four; the remaining PCRE spelling differences (\h, \v, \pL,
\xHH in classes, ...) need a pattern rewrite or an engine change, see
#60.
Reproduction
Rust (incorrect)
$ printf 'C\n' | ./target/release/grep -P 'a(?i)b|c'
# Exit code: 1
$ printf 'Host: x\n' | ./target/release/grep -P '(?<=Host: |host=)x'
grep: invalid pattern "(?<=Host: |host=)x": invalid pattern in look-behind
# Exit code: 2
$ printf 'ab ab\n' | ./target/release/grep -P '(?<w>\w+) \1'
grep: invalid pattern "(?<w>\w+) \1": numbered backref/call is not allowed. (use name)
# Exit code: 2
$ printf '\xd9\xa0\n' | ./target/release/grep -cP '\d'
1
# Exit code: 0GNU (correct)
$ printf 'C\n' | LC_ALL=C /usr/bin/grep -P 'a(?i)b|c'
C
# Exit code: 0
$ printf 'Host: x\n' | LC_ALL=C /usr/bin/grep -P '(?<=Host: |host=)x'
Host: x
# Exit code: 0
$ printf 'ab ab\n' | LC_ALL=C /usr/bin/grep -P '(?<w>\w+) \1'
ab ab
# Exit code: 0
$ printf '\xd9\xa0\n' | LC_ALL=C /usr/bin/grep -cP '\d'
0
# Exit code: 1(\d gives the same GNU result under LC_ALL=C.UTF-8.)
Root cause
CompiledPattern::compile (src/matcher.rs:270-274):
RegexMode::Perl => Syntax::perl_ng(),
...
syntax.enable_behavior(SyntaxBehavior::SYNTAX_BEHAVIOR_ALLOW_INTERVAL_LOW_ABBREV);Perl_NGincludesONIG_SYN_ISOLATED_OPTION_CONTINUE_BRANCH(the(?i)case), but theonig
crate'senable_behaviorreads the current bits throughSyntaxBehavior::from_bits_truncate,
and the crate has no constant for that bit, so it is silently dropped.Perl_NGsetsONIG_SYN_CAPTURE_ONLY_NAMED_GROUP(numbered back-refs rejected next to named
groups) and lacksONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND.- No
ONIG_OPTION_DIGIT_IS_ASCIIis passed for Perl, so\dis Unicode.
Test
Extend pcre_features in tests/test_grep.rs:
let (_s, mut c) = ucmd();
c.args(&["-P", "a(?i)b|c"]).pipe_in("C\n").succeeds().stdout_only("C\n");
let (_s, mut c) = ucmd();
c.args(&["-P", "-o", r"(?<=Host: |host=)\w+"])
.pipe_in("Host: one\nhost=two\n")
.succeeds()
.stdout_only("one\ntwo\n");
let (_s, mut c) = ucmd();
c.args(&["-P", r"(?<w>\w+) \1"]).pipe_in("ab ab\nab cd\n").succeeds().stdout_only("ab ab\n");
let (_s, mut c) = ucmd();
c.args(&["-cP", r"\d"]).pipe_in("\u{0660}\n").fails_with_code(1).stdout_only("0\n");Reported by Anthropic's OSS Scanner (ANT-2026-MK0HLNFN).
Metadata
Metadata
Assignees
Labels
bugSomething isn't workinggnu-compatBehaviour differs from GNUgood first issueGood for newcomersoss-scannerFound by Anthropic's OSS Scanner
Activity