Repository navigation
Description
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: 2GNU (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: 2Root 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
fix(matcher): reject chained range operators in bracket expressions
fix(matcher): reject chained range operators in bracket expressions