Skip to content

grep: report error instead of panicking on Oniguruma retry limit - #228

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:fix-grep-backtracking-limit-error
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:fix-grep-backtracking-limit-error

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

When matching complex regular expressions with nested repetition (such as ((x+)*)+$) against non-matching lines, Oniguruma exhausts its retry limit and returns ONIGERR_RETRY_LIMIT_IN_MATCH_OVER (or ONIGERR_MATCH_STACK_LIMIT_OVER).

Previously, onig_match_result panicked on any negative return code other than ONIG_MISMATCH, causing uu_grep to abort with SIGABRT (exit 134). GNU grep instead treats this condition as an execution failure, emitting grep: <file>: exceeded PCRE's backtracking limit and exiting with status 2.

This PR propagates Oniguruma matching errors as io::Error through Matcher::match_line and Matcher::is_match up to the searcher session:

  1. onig_match_result maps ONIGERR_RETRY_LIMIT_IN_MATCH_OVER and ONIGERR_MATCH_STACK_LIMIT_OVER to io::Error::other("exceeded PCRE's backtracking limit") in PCRE mode (-P), or io::Error::other("exceeded backtracking limit") in other modes.
  2. Matcher::match_line, Matcher::is_match, and MatchIter propagate matching errors rather than discarding or panicking.
  3. The searcher records the failure via report_io_error and exits with code 2.

Fixes #191

@codspeed

codspeed Bot commented Nov 20, 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:fix-grep-backtracking-limit-error (0ee6081) with main (344d061)

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 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.62366% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.80%. Comparing base (344d061) to head (0ee6081).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/matcher.rs 94.44% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #228      +/-   ##
==========================================
- Coverage   96.89%   96.80%   -0.10%     
==========================================
  Files           6        6              
  Lines        2160     2191      +31     
  Branches      235      240       +5     
==========================================
+ Hits         2093     2121      +28     
- Misses         67       70       +3     
Flag Coverage Δ
macOS_latest 97.74% <94.62%> (-0.11%) ⬇️
ubuntu_latest 97.74% <94.62%> (-0.11%) ⬇️
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: 76 / FAILED: 31 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 75 / FAILED: 32 / SKIPPED: 21

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

Test improvements (1):
  + pcre-abort

Comment thread tests/test_grep.rs Outdated
.no_stdout()
.no_stderr();

// In -E mode, hitting the retry limit also yields an error and exit code 2, not a panic.

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.

GNU finds the match with -E (see #191)
this test locks in a message GNU never prints
could you please either fix it for -E or leave that case out and keep #191 open for it?

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.

Left the -E case out of the test so #191 can stay open for it.

Comment thread src/matcher.rs Outdated
/// entire haystack to find the longest. This makes it unsuitable for searching,
/// but it's perfect for a second, anchored match pass for POSIX semantics.
longest_anchored: OnigRegex,
is_pcre: 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.

do we need to carry is_pcre through search/match_at/onig_match_result just for the message?
could return a small error enum and pick the text in Matcher, which already has the config

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: a MatchError enum is returned, and Matcher picks the text from the config.

Comment thread src/matcher.rs Outdated
break;
}
}
let raw_matched = any;

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 any and raw_matched?
try_fold or a direct early return would be simpler

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.

Simplified to a direct early return.

Comment thread src/matcher.rs Outdated
let res = self
.leftmost
.search(line, offset, Some(&mut region), self.is_pcre)?;
if res.is_some() {

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.

Ok(res.and(region.pos(0))), no?

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.

Yes, now Ok(res.and(region.pos(0))).

Comment thread tests/test_grep.rs Outdated
// See: https://github.com/uutils/grep/issues/191
let (_s, mut c) = ucmd();
c.args(&["-P", "((m+)*)+$"])
.pipe_in(format!("{}n\n", "m".repeat(45)))

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.

same format!("{}n\n", "m".repeat(45)) 5 times, please put it in a variable

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.

Pulled into a single variable.

Comment thread tests/test_grep.rs Outdated
.no_stdout()
.stderr_does_not_contain("panicked");

// -s suppresses the diagnostic on stderr and still fails with code 2.

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.

did you check that GNU really hides this message with -s?

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.

I left the -s subcase out of the test as well, rather than lock in something I hadn't fully checked against GNU.

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-backtracking-limit-error branch from 4bbd334 to a16c779 Compare November 22, 2026 13:20
@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Updated in a16c779:

  • Replaced boolean passing with a dedicated MatchError enum; Matcher formats the diagnostic according to config.regex_mode.
  • Streamlined is_match with direct early return.
  • Used Ok(res.and(region.pos(0))) in search_leftmost.
  • Extracted repeated test input string into a single variable.
  • Left out the -E and -s subcases from the test to keep grep - a pattern that hits Oniguruma's retry limit panics and aborts (exit 134) #191 open for -E.

@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):
  + pcre-abort

@yunusarda-sudo
yunusarda-sudo force-pushed the fix-grep-backtracking-limit-error branch from a16c779 to 0ee6081 Compare November 23, 2026 20:55
@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):
  + pcre-abort

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 - a pattern that hits Oniguruma's retry limit panics and aborts (exit 134)

2 participants