Skip to content

grep: read an unmatched ) in -E as a literal - #150

Open
jayhemnani9910 wants to merge 1 commit into
uutils:mainfrom
jayhemnani9910:ere-unmatched-close-paren
Open

jayhemnani9910 wants to merge 1 commit into
uutils:mainfrom
jayhemnani9910:ere-unmatched-close-paren

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

GNU grep -E takes a ) that closes no group as an ordinary character. Here the pattern is rejected:

$ printf 'a)\nb\n' | grep -E 'a)'
a)                                                         # GNU grep 3.12
grep: invalid pattern "a)": unmatched close parenthesis    # main, exit 2

Same for ), (a)), b|), ^), )* and so on. Oniguruma has a syntax flag for exactly this (ALLOW_UNMATCHED_CLOSE_SUBEXP, on in its own POSIX extended syntax), so the fix turns it on for -E only. In a basic regexp an unmatched \) is still an error, as in GNU; ) there was already a literal.

New test ere_unmatched_close_paren_is_literal (fails on main). cargo test: test_grep 104 pass (103 on main), clippy and fmt clean. Against GNU grep 3.12, 33 -E patterns with unmatched, nested, escaped, bracketed and repeated parentheses give the same output and exit status, and the BRE cases (a\), \(a\)\), ), \)) give the same exit status.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 03:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Oct 26, 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 jayhemnani9910:ere-unmatched-close-paren (d5f0828) with main (0be8c54)

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. ↩

Comment thread tests/test_grep.rs Outdated
Comment on lines +150 to +153
(")", "a)\n(a)\n)\n"),
("a)", "a)\n(a)\n"),
("(a))", "a)\n(a)\n"),
("b|)", "a)\n(a)\n)\nb\n"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems a bit excessive. Are we unit testing the regex crate? :D

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.

Fair, cut it down to one case.

GNU grep -E treats a closing parenthesis with no open group as an
ordinary character, so `grep -E 'a)'` matches "a)". We rejected the
pattern with "unmatched close parenthesis". Basic regexps keep
rejecting an unmatched \).
@jayhemnani9910
jayhemnani9910 force-pushed the ere-unmatched-close-paren branch from d06bc81 to d5f0828 Compare October 27, 2026 17:23
Copilot AI balanced review requested due to automatic review settings October 27, 2026 17:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

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.

3 participants