Skip to content

grep - -E accepts a second range operator like [a-c-e] that GNU rejects #163

Open

Description

@sylvestre

Known: #71 (merged PR) — #71

A bracket expression cannot chain ranges: in [a-c-e] the second - has a range as its left
endpoint, and GNU grep rejects it with Invalid range end (exit 2) in both basic and extended
mode. uu_grep with -E accepts it and reads it as [a-c], -, e, so the pattern runs
silently with a different meaning. In basic mode uu_grep already rejects it, but with
Oniguruma's message instead of GNU's.

(This is part 2 of the scanner report; part 1, range ordering under non-C LC_COLLATE, is
locale work and not a good first issue.)

Reproduction

Rust (incorrect)

$ printf 'd\n-\n' | ./target/release/grep -E '[a-c-e]'
-
# Exit code: 0

$ printf 'd\n-\n' | ./target/release/grep '[a-c-e]'
grep: invalid pattern "[a-c-e]": unmatched range specifier in char-class
# Exit code: 2

GNU (correct)

$ printf 'd\n-\n' | LC_ALL=C /usr/bin/grep -E '[a-c-e]'
grep: Invalid range end
# Exit code: 2

$ printf 'd\n-\n' | LC_ALL=C /usr/bin/grep '[a-c-e]'
grep: Invalid range end
# Exit code: 2

Root cause

src/matcher.rs:269 selects Syntax::gnu_regex() for -E. Its behaviour bits include
ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC ([0-9-a] = [0-9\-a]), which Syntax::grep() (basic
mode) does not set. Then compile_with (src/matcher.rs:333) only translates
ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS into Invalid range end:

let message = match err.code {
    ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS => "Invalid range end".to_string(),
    _ => format!("invalid pattern \"{pattern}\": {}", err.message),
};

Test

Extend reversed_range_endpoints_match_gnu_message in tests/test_grep.rs:

for args in [&["[p-r-t]"][..], &["-E", "[p-r-t]"][..]] {
    let (_s, mut c) = ucmd();
    c.args(args)
        .pipe_in("s\n-\n")
        .fails_with_code(2)
        .no_stdout()
        .stderr_is("grep: Invalid range end\n");
}
let (_s, mut c) = ucmd();
c.args(&["-E", "[p-r-]"]).pipe_in("q\n-\nz\n").succeeds().stdout_only("q\n-\n");

Reported by Anthropic's OSS Scanner (ANT-2026-KN6SD3ON).

Activity

  1. added
    bug
    good first issue
    oss-scanner
    gnu-compat
    on Nov 9, 2026
  2. added 2 commits that reference this issue on Nov 25, 2026
    471cbb6
    28cde0d
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