Skip to content

fix: reject multiple patterns when -P/--perl-regexp is used - #40

Merged
sylvestre merged 1 commit into
uutils:mainfrom
koopatroopa787:issue-34-perl-single-pattern
Jun 5, 2026
Merged

fix: reject multiple patterns when -P/--perl-regexp is used#40
sylvestre merged 1 commit into
uutils:mainfrom
koopatroopa787:issue-34-perl-single-pattern

Conversation

@koopatroopa787

Copy link
Copy Markdown
Contributor

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`:

  1. Two `-e` flags with `-P` → exit 2 + error message
  2. Newline inside a single `-e` pattern string with `-P` → exit 2 + error message
  3. Single `-e` with `-P` → still works correctly

All tests pass (`cargo test perl_regexp_rejects_multiple_patterns`).

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
Copilot AI review requested due to automatic review settings June 5, 2026 00:26

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.

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 -P fails with exit code 2 when given multiple patterns.
  • Add runtime validation in uumain to reject multiple patterns when -P is 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 thread src/lib.rs
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(),
));
}
Comment thread tests/test_grep.rs
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");
Comment thread tests/test_grep.rs
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");
Comment thread src/lib.rs
if perl_regexp && patterns.len() > 1 {
return Err(USimpleError::new(
2,
"the -P option only supports a single pattern".to_string(),
@codspeed-hq

codspeed-hq Bot commented Jun 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing koopatroopa787:issue-34-perl-single-pattern (e3d80f5) with main (f4798cb)

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.

@sylvestre
sylvestre merged commit f5d5f6c into uutils:main Jun 5, 2026
14 checks passed
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.

-P accepts multiple patterns where GNU allows only one

3 participants