Skip to content

grep - -P rejects or misreads some everyday PCRE patterns because of Oniguruma syntax bits #182

Open

Description

@sylvestre

Known: #60 (open issue) — #60

-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 the c branch, so C is 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.
  • \d matches non-ASCII digits such as U+0660; in GNU -P, \d is [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: 0

GNU (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_NG includes ONIG_SYN_ISOLATED_OPTION_CONTINUE_BRANCH (the (?i) case), but the onig
    crate's enable_behavior reads the current bits through SyntaxBehavior::from_bits_truncate,
    and the crate has no constant for that bit, so it is silently dropped.
  • Perl_NG sets ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP (numbered back-refs rejected next to named
    groups) and lacks ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND.
  • No ONIG_OPTION_DIGIT_IS_ASCII is passed for Perl, so \d is 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).

Activity

  1. added
    bug
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issueoss-scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions