From 795a52097cc7bb9cdd4ea0f6d33d814307cf1871 Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Wed, 25 Nov 2026 19:13:25 +0300 Subject: [PATCH 1/2] grep: reject non-POSIX and unclosed [:name:] classes in BRE/ERE Fixes #171. --- src/matcher.rs | 72 ++++++++++++++++++++++++++++++++-------------- tests/test_grep.rs | 30 +++++++++++++++++++ 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/src/matcher.rs b/src/matcher.rs index 42405a3..94db7b9 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -318,12 +318,9 @@ impl CompiledPattern { } if matches!(config.regex_mode, RegexMode::Basic | RegexMode::Extended) - && has_confusing_bracket(pattern.as_bytes()) + && let Err(msg) = check_brackets(pattern.as_bytes()) { - return Err(USimpleError::new( - 2, - "character class syntax is [[:space:]], not [:space:]".to_string(), - )); + return Err(USimpleError::new(2, msg.to_string())); } if config.regex_mode == RegexMode::Extended && has_empty_interval(pattern.as_bytes()) { @@ -600,7 +597,10 @@ fn has_empty_interval(pattern: &[u8]) -> bool { continue; } b'[' => { - i = scan_bracket(pattern, i + 1).1; + i = match scan_bracket(pattern, i + 1) { + Ok((_, next)) => next, + Err(_) => return false, + }; after_atom = true; continue; } @@ -617,32 +617,56 @@ fn has_empty_interval(pattern: &[u8]) -> bool { } /// True when `pattern` holds a bracket expression that looks like a misspelled -/// character class, e.g. `[:space:]` instead of `[[:space:]]`. GNU grep rejects -/// those: a bracket whose first and last characters are colons, that holds at -/// least one other character, and that contains no range, class, equivalence -/// class or collating element. -fn has_confusing_bracket(pattern: &[u8]) -> bool { +fn is_posix_char_class(name: &[u8]) -> bool { + matches!( + name, + b"alnum" + | b"alpha" + | b"blank" + | b"cntrl" + | b"digit" + | b"graph" + | b"lower" + | b"print" + | b"punct" + | b"space" + | b"upper" + | b"xdigit" + ) +} + +/// Validate bracket expressions in BRE/ERE patterns: +/// - Reject `[:space:]` used without outer `[...]` +/// - Reject non-POSIX `[:name:]` classes +/// - Reject unclosed `[:`, `[.`, `[=` subexpressions inside `[...]` +fn check_brackets(pattern: &[u8]) -> Result<(), &'static str> { let mut i = 0; while i < pattern.len() { match pattern[i] { b'\\' => i += 2, b'[' => { - let (confusing, next) = scan_bracket(pattern, i + 1); + let (confusing, next) = scan_bracket(pattern, i + 1)?; if confusing { - return true; + return Err("character class syntax is [[:space:]], not [:space:]"); } i = next; } _ => i += 1, } } - false + Ok(()) +} + +#[cfg(test)] +fn has_confusing_bracket(pattern: &[u8]) -> bool { + check_brackets(pattern) == Err("character class syntax is [[:space:]], not [:space:]") } /// Scan the body of a bracket expression starting at `start` (just past the /// `[`). Returns whether it is a misspelled character class and the index just -/// past its closing `]`. -fn scan_bracket(pattern: &[u8], start: usize) -> (bool, usize) { +/// past its closing `]`, or a GNU-compatible diagnostic on invalid/unclosed +/// `[:...:]`, `[....]`, `[=...=]` subexpressions. +fn scan_bracket(pattern: &[u8], start: usize) -> Result<(bool, usize), &'static str> { const FIRST_IS_COLON: u8 = 1; const LAST_IS_COLON: u8 = 2; const HAS_OTHER: u8 = 4; @@ -662,18 +686,22 @@ fn scan_bracket(pattern: &[u8], start: usize) -> (bool, usize) { let c = pattern[i]; // A `]` right at the start of the body is an ordinary character. if c == b']' && i != body_start { - return (state == FIRST_IS_COLON | LAST_IS_COLON | HAS_OTHER, i + 1); + return Ok((state == FIRST_IS_COLON | LAST_IS_COLON | HAS_OTHER, i + 1)); } // Only the character just before the closing `]` counts as the last one. state &= !LAST_IS_COLON; // `[:alpha:]`, `[.a.]` and `[=a=]` inside the bracket. if c == b'[' && matches!(pattern.get(i + 1), Some(b':' | b'.' | b'=')) { let delimiter = pattern[i + 1]; - if let Some(end) = find_bracket_subexpr_end(pattern, i + 2, delimiter) { - state |= HAS_RANGE_OR_CLASS; - i = end; - continue; + let Some(end) = find_bracket_subexpr_end(pattern, i + 2, delimiter) else { + return Err("Unmatched [, [^, [:, [., or [="); + }; + if delimiter == b':' && !is_posix_char_class(&pattern[i + 2..end - 2]) { + return Err("Invalid character class name"); } + state |= HAS_RANGE_OR_CLASS; + i = end; + continue; } // `x-y` is a range, but the `-` of `[x-]` is an ordinary character. if pattern.get(i + 1) == Some(&b'-') @@ -687,7 +715,7 @@ fn scan_bracket(pattern: &[u8], start: usize) -> (bool, usize) { i += 1; } // Unterminated bracket: the regex engine reports that on its own. - (false, pattern.len()) + Ok((false, pattern.len())) } /// Index just past the `:]`, `.]` or `=]` closing a `[: [. [=` subexpression diff --git a/tests/test_grep.rs b/tests/test_grep.rs index e2cbbeb..d0aebf0 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -2697,3 +2697,33 @@ fn diagnostics_follow_earlier_output() { "a.txt:foo a\ngrep: bin.dat: binary file matches\ngrep: missing.txt: No such file or directory\nb.txt:foo b\n" ); } + +#[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 [="), + ("[[:]", "Unmatched [, [^, [:, [., or [="), + ("[[:a:b]", "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"); +} From 4eb6988f3e3d28d589c9cfd63fde61bcc0d2253b Mon Sep 17 00:00:00 2001 From: Yunus Ardaguney Date: Thu, 26 Nov 2026 01:35:25 +0300 Subject: [PATCH 2/2] test: use fixture files instead of pipe_in in rejects_invalid_character_class_names --- tests/test_grep.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_grep.rs b/tests/test_grep.rs index d0aebf0..311cc74 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -2712,18 +2712,18 @@ fn rejects_invalid_character_class_names() { ]; for (pattern, message) in cases { for mode in ["-G", "-E"] { - let (_s, mut c) = ucmd(); - c.args(&[mode, pattern]) - .pipe_in("7\n:]\n") + let (scene, mut c) = ucmd(); + scene.fixtures.write("input", "7\n:]\n"); + c.args(&[mode, pattern, "input"]) .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") + let (scene, mut c) = ucmd(); + scene.fixtures.write("valid", "f\nz\n"); + c.args(&["[[:xdigit:]]", "valid"]) .succeeds() .stdout_only("f\n"); }