Skip to content

grep: reject interval counts above 32767 - #218

Open
yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-interval-limit
Open

yunusarda-sudo wants to merge 1 commit into
uutils:mainfrom
yunusarda-sudo:grep-interval-limit

Conversation

@yunusarda-sudo

Copy link
Copy Markdown
Contributor

GNU grep rejects an interval count above 32767, in either bound and in both BRE and ERE, with Regular expression too big (exit 2). Oniguruma's own limit is 100000, so k{40000} and k{1,40000} were searched.

The pattern is now scanned for intervals before compiling (skipping escapes and bracket expressions, reusing scan_bracket). Braces that don't form an interval ([{]40000}, BRE k{40000}, an unclosed k{40000) are left alone.

Closes #208

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.81%. Comparing base (344d061) to head (68e4976).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/matcher.rs 93.33% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #218      +/-   ##
==========================================
- Coverage   96.89%   96.81%   -0.09%     
==========================================
  Files           6        6              
  Lines        2160     2196      +36     
  Branches      235      241       +6     
==========================================
+ Hits         2093     2126      +33     
- Misses         67       70       +3     
Flag Coverage Δ
macOS_latest 97.74% <93.33%> (-0.11%) ⬇️
ubuntu_latest 97.74% <93.33%> (-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.

@codspeed

codspeed Bot commented Nov 17, 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-interval-limit (68e4976) 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. ↩

Comment thread src/matcher.rs Outdated
let count = pattern[i..i + digits].iter().try_fold(0u64, |n, &b| {
n.checked_mul(10)?.checked_add(u64::from(b - b'0'))
});
too_big |= count.is_none_or(|n| n > MAX_INTERVAL_COUNT) && digits > 0;

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.

&& digits > 0 is dead: with no digits the fold returns Some(0)
also the loop/continue could be two plain parses (min, optional ,, max), 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.

Fixed in commit fe9c825: simplified to split on , into min and max bounds without the loop, and removed the redundant digits > 0 check.

Comment thread src/matcher.rs Outdated
let mut too_big = false;
let mut i = start;
let mut has_comma = false;
loop {

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 loop with a single continue is hard to follow
could you take the body up to close and split it on , instead?

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 in commit fe9c825: the body up to close is now extracted and split on , directly.

Comment thread src/matcher.rs Outdated
let count = pattern[i..i + digits].iter().try_fold(0u64, |n, &b| {
n.checked_mul(10)?.checked_add(u64::from(b - b'0'))
});
too_big |= count.is_none_or(|n| n > MAX_INTERVAL_COUNT) && digits > 0;

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.

&& digits > 0 is not needed, no digits gives Some(0)

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.

Removed in commit fe9c825.

Comment thread src/matcher.rs Outdated
/// Whether `pattern` has an interval (`{m,n}` in extended mode, `\{m,n\}` in
/// basic mode) with a count above [`MAX_INTERVAL_COUNT`]. Oniguruma's own
/// limit is higher, so GNU's has to be checked here.
fn has_interval_over_limit(pattern: &[u8], extended: bool) -> 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.

almost the same pass as has_confusing_bracket, could it be one pass over the pattern?

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.

Unified in commit fe9c825: combined into a single precheck_pattern pass handling both confusing brackets and interval limits.

@yunusarda-sudo
yunusarda-sudo force-pushed the grep-interval-limit branch 2 times, most recently from 0d6fb93 to 506a5dc Compare November 22, 2026 12:16

@lhecker lhecker left a comment

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.

Is it really worth testing for this? The limit seems like a quirk and not like a feature that is inherently needed.

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

The check itself is there to match GNU grep: interval counts above 32767 (POSIX RE_DUP_MAX) are rejected with Regular expression too big (exit 2). Oniguruma's own limit is 100000, so without this we'd accept k{40000} and k{1,40000} that GNU rejects.

The test is the regression for that bound — including a case that is accepted (k{0,32767}) and braces that are not intervals. I'm happy to drop it or slim it to a single oversized-count case if you'd rather keep the PR thinner.

@lhecker

lhecker commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

That's literally what the issue and PR already say. 🫡

Now regarding my point...

@yunusarda-sudo

Copy link
Copy Markdown
Contributor Author

Fair point — dropped the test.

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 - interval counts above 32767 are accepted; GNU rejects them with Regular expression too big

3 participants