Skip to content

grep: reject non-POSIX and unclosed [:name:] classes in BRE/ERE - #253

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-validate-posix-char-classes
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-validate-posix-char-classes

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

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 code 2. An unclosed [:, [., or [= inside a bracket expression (like [[:alpha]] or [x[:]y]) fails with Unmatched [, [^, [:, [., or [=.

Previously, scan_bracket only checked for misspelled [:space:] syntax without validating the class name or reporting unclosed subexpressions, allowing non-POSIX classes to pass through to Oniguruma.

This patch:

  • Validates POSIX class names inside [:...:] and errors with Invalid character class name on non-POSIX/negated names.
  • Reports Unmatched [, [^, [:, [., or [= when [:, [., or [= is not properly closed before the end of the bracket.
  • Preserves the existing [:space:] diagnostic.

Fixes #171

Investigated and verified locally with cargo fmt --all -- --check, cargo clippy -- -D warnings, and cargo test.

@codspeed

codspeed Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing yunusarda-sudo:grep-validate-posix-char-classes (fac440d) with main (280669a)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.38%. Comparing base (280669a) to head (fac440d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #253      +/-   ##
==========================================
+ Coverage   97.32%   97.38%   +0.06%     
==========================================
  Files           6        6              
  Lines        2316     2331      +15     
  Branches      250      250              
==========================================
+ Hits         2254     2270      +16     
+ Misses         62       61       -1     
Flag Coverage Δ
macOS_latest 98.26% <100.00%> (+0.05%) ⬆️
ubuntu_latest 98.26% <100.00%> (+0.05%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + ere

1 similar comment
@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 76 / FAILED: 31 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + ere

@github-actions

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 78 / FAILED: 29 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 77 / FAILED: 30 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + ere

Comment thread src/matcher.rs
fn has_confusing_bracket(pattern: &[u8]) -> bool {
fn is_posix_char_class(name: &[u8]) -> bool {
matches!(
name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is a leftover of the old doc comment, it now sits on is_posix_char_class.
please fix it, and keep the explanation of what a "confusing" bracket is somewhere (on scan_bracket?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: the stray line is gone, and the "confusing bracket" explanation now lives on scan_bracket.

Comment thread src/matcher.rs Outdated
#[cfg(test)]
fn has_confusing_bracket(pattern: &[u8]) -> bool {
check_brackets(pattern) == Err("character class syntax is [[:space:]], not [:space:]")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to keep this only for the tests?
could the unit tests call check_brackets directly instead of comparing the message string?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: has_confusing_bracket is removed, the unit tests call check_brackets directly.

Comment thread src/matcher.rs
}
i = next;
}
_ => i += 1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this message is now written twice, could it be a const?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the three messages are consts now.

Comment thread src/matcher.rs
@@ -617,32 +614,56 @@ fn has_empty_interval(pattern: &[u8]) -> bool {
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_brackets runs before has_empty_interval, so this branch can't be reached, no?
probably the line codecov is complaining about

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scan_bracket returns Result<usize, _> now, so the dummy end index on error is gone; has_empty_interval only falls back to pattern.len() for brackets check_brackets already accepted. Say if you meant another branch.

Comment thread tests/test_grep.rs
("[[:^digit:]]", "Invalid character class name"),
("[[::]]", "Invalid character class name"),
("[[:digit]]", "Unmatched [, [^, [:, [., or [="),
("[x[:]y]", "Unmatched [, [^, [:, [., or [="),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add cases for unclosed [. and [= too, like [[.a] and [[=a]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[[.a] and [[=a] are in the table in rejects_invalid_character_class_names (for -G and -E); I also added a unit test that calls check_brackets on them.

…uutils#171)

GNU grep accepts only the twelve POSIX class names inside a bracket expression. Any other name, such as `[[:word:]]`, `[[:^alpha:]]` or `[[::]]`, fails with `Invalid character class name` and exit code 2. An unclosed `[:`, `[.` or `[=` inside a bracket fails with `Unmatched [, [^, [:, [., or [=`.
@yunusarda-sudo
yunusarda-sudo force-pushed the grep-validate-posix-char-classes branch from 7289229 to fac440d Compare December 5, 2026 04:55
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 79 / FAILED: 28 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 78 / FAILED: 29 / SKIPPED: 21

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + ere

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

The cygwin failure is the known PTY flake (color_on_a_terminal_needs_option_and_term_not_dumb), unrelated to this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - accepts non-POSIX and malformed [:name:] classes that GNU rejects

2 participants