Repository navigation
Description
Known: #100 (merged PR) — #100
GNU grep accepts only the twelve POSIX class names inside a bracket expression (alnum,
alpha, blank, cntrl, digit, graph, lower, print, punct, space, upper,
xdigit). Any other name, such as [[:word:]], [[:ascii:]], the negated [[:^alpha:]] or
the empty [[::]], fails with Invalid character class name and exit 2. A [: inside a
bracket that is not closed by :] (a typo like [[:alpha]]) fails with
Unmatched [, [^, [:, [., or [=.
uu_grep (in -G and -E) passes these through to Oniguruma, which knows word/ascii,
accepts ^ negation, and reads a malformed [: as the plain characters [ and :. So the
pattern runs silently with a different meaning: [[:alpha]] selects the line :] instead of
reporting the typo.
Reproduction
Rust (incorrect)
$ printf 'a\nb\n' | ./target/release/grep '[[:word:]]'
a
b
# Exit code: 0
$ printf 'a\n:]\n' | ./target/release/grep '[[:alpha]]'
:]
# Exit code: 0GNU (correct)
$ printf 'a\nb\n' | LC_ALL=C /usr/bin/grep '[[:word:]]'
grep: Invalid character class name
# Exit code: 2
$ printf 'a\n:]\n' | LC_ALL=C /usr/bin/grep '[[:alpha]]'
grep: Unmatched [, [^, [:, [., or [=
# Exit code: 2Same results for -E. [[:ascii:]], [[:^alpha:]] and [[::]] give
Invalid character class name in GNU; [[:], [a[:] and [[:a:b] give
Unmatched [, [^, [:, [., or [=. Unknown alphabetic names like [[:foo:]] are already
rejected by both. -F and -P are not affected.
Root cause
src/matcher.rs:281 (in CompiledPattern::compile) runs the only pre-check on bracket
expressions:
if matches!(config.regex_mode, RegexMode::Basic | RegexMode::Extended)
&& has_confusing_bracket(pattern.as_bytes())
{
return Err(USimpleError::new(
2,
"character class syntax is [[:space:]], not [:space:]".to_string(),
));
}Its helper scan_bracket (src/matcher.rs:563) already walks every bracket expression and
finds each [:...:] via find_bracket_subexpr_end, but it only uses that to detect the
[:space:] typo; it never checks the name, and when no :] is found it just keeps going.
Test
#[test]
fn rejects_invalid_character_class_names() {
let cases = [
("[[:word:]]", "Invalid character class name"),
("[[:ascii:]]", "Invalid character class name"),
("[[:^digit:]]", "Invalid character class name"),
("[[::]]", "Invalid character class name"),
("[[:digit]]", "Unmatched [, [^, [:, [., or [="),
("[x[:]y]", "Unmatched [, [^, [:, [., or [="),
];
for (pattern, message) in cases {
for mode in ["-G", "-E"] {
let (_s, mut c) = ucmd();
c.args(&[mode, pattern])
.pipe_in("7\n:]\n")
.fails_with_code(2)
.no_stdout()
.stderr_contains(message);
}
}
// The twelve POSIX names keep working.
let (_s, mut c) = ucmd();
c.args(&["[[:xdigit:]]"]).pipe_in("f\nz\n").succeeds().stdout_only("f\n");
}Reported by Anthropic's OSS Scanner (ANT-2026-5S1BPG4T).
Activity