fix: reject multiple patterns when -P/--perl-regexp is used - #40
Merged
sylvestre merged 1 commit intoJun 5, 2026
Merged
Conversation
GNU grep's PCRE backend supports only a single pattern. Supplying
multiple patterns via repeated -e flags, or a pattern string that
contains a literal newline, must exit 2 with the message:
the -P option only supports a single pattern
Add the validation immediately after patterns are collected, before
regex-mode selection. Add a test covering:
- two separate -e flags with -P
- a newline-embedded pattern string with -P
- single -e with -P still works normally
Closes uutils#34
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds GNU grep–compatible behavior for PCRE mode (-P) by rejecting multiple patterns and validating it via new regression tests.
Changes:
- Add a new test ensuring
-Pfails with exit code 2 when given multiple patterns. - Add runtime validation in
uumainto reject multiple patterns when-Pis active.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/test_grep.rs | Adds a regression test covering -P single-pattern restriction and expected error output. |
| src/lib.rs | Enforces “single pattern only” behavior for PCRE mode by returning exit code 2 with a specific message. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+258
to
+264
| // GNU grep's PCRE backend (-P) supports only a single pattern. | ||
| if perl_regexp && patterns.len() > 1 { | ||
| return Err(USimpleError::new( | ||
| 2, | ||
| "the -P option only supports a single pattern".to_string(), | ||
| )); | ||
| } |
| c.args(&["-P", "-e", "foo", "-e", "bar"]) | ||
| .pipe_in("foo\nbar\n") | ||
| .fails_with_code(2) | ||
| .stderr_contains("the -P option only supports a single pattern"); |
| c.args(&["-P", "-e", "foo\nbar"]) | ||
| .pipe_in("foo\nbar\n") | ||
| .fails_with_code(2) | ||
| .stderr_contains("the -P option only supports a single pattern"); |
| if perl_regexp && patterns.len() > 1 { | ||
| return Err(USimpleError::new( | ||
| 2, | ||
| "the -P option only supports a single pattern".to_string(), |
Merging this PR will not alter performance
Comparing Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #34
Problem
GNU grep rejects multiple patterns when `-P` (PCRE mode) is used:
```
$ grep -P -e foo -e bar file
grep: the -P option only supports a single pattern
```
`uu_grep` was silently accepting multiple patterns and searching for all of them, diverging from GNU grep's behaviour.
Fix
Added a validation check after patterns are collected (in `src/lib.rs`) — if `--perl-regexp` is set and more than one pattern was accumulated (from multiple `-e` flags or a newline-embedded pattern string), return exit code `2` with the canonical error message.
Tests
Three cases added to `tests/test_grep.rs::perl_regexp_rejects_multiple_patterns`:
All tests pass (`cargo test perl_regexp_rejects_multiple_patterns`).