Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{Config, RegexMode};
use memchr::memmem;
use onig::{RegexOptions, Region, SearchOptions, Syntax, SyntaxBehavior, SyntaxOperator};
use onig_sys::{
ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS, OnigEncCtype_ONIGENC_CTYPE_WORD, OnigEncodingUTF8,
ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS, ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE,
OnigEncCtype_ONIGENC_CTYPE_WORD, OnigEncodingUTF8,
};
use std::ptr::{null, null_mut};
use std::sync::Mutex;
Expand Down Expand Up @@ -298,6 +299,24 @@ impl CompiledPattern {
pattern
};

// Oniguruma reads `{2,1}` as a possessive `{1,2}`, GNU grep rejects it.
// PLUS_POSSESSIVE_INTERVAL makes Oniguruma reject it too, but it also
// makes `{n,m}+` possessive, so it's only used for this check.
if matches!(config.regex_mode, RegexMode::Basic | RegexMode::Extended)
&& pattern.contains('{')
{
let mut strict = syntax;
strict.enable_operators(SyntaxOperator::SYNTAX_OPERATOR_PLUS_POSSESSIVE_INTERVAL);
if let Err(err) = OnigRegex::compile(pattern, &strict, RegexOptions::REGEX_OPTION_NONE)
&& err.code == ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
{
return Err(USimpleError::new(
2,
"Invalid content of \\{\\}".to_string(),
));
}
}

if config.regex_mode == RegexMode::Perl {
// GNU grep supports `(?P<name>...)`.
// Unfortunately, the onig crate defines the OP2 flag without the
Expand Down
22 changes: 22 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ fn misspelled_character_class_is_error() {
}
}

#[test]
fn reversed_interval_is_error() {
for args in [&["-E", "fo{2,1}"][..], &["fo\\{2,1\\}"][..]] {

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.

could you please also add the cases that must keep working, [{2,1}] and ^fo{1,2}+$ matching foo?
they are why the strict compile is only a check

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.

Added in 17be37a: [{2,1}] matches { and ^fo{1,2}+$ matches foo, in both -E and basic syntax.

let (_s, mut c) = ucmd();
c.args(args)
.fails_with_code(2)
.stderr_contains("Invalid content of \\{\\}");
}

for args in [&["-E", "[{2,1}]"][..], &["[{2,1}]"][..]] {
let (_s, mut c) = ucmd();
c.args(args).pipe_in("{\n").succeeds().stdout_only("{\n");
}
for args in [&["-E", "^fo{1,2}+$"][..], &["^fo\\{1,2\\}\\+$"][..]] {
let (_s, mut c) = ucmd();
c.args(args)
.pipe_in("foo\n")
.succeeds()
.stdout_only("foo\n");
}
}

#[test]
fn max_count_rewinds_seekable_stdin() {
// -m stops mid-input after reading ahead. When standard input can seek,
Expand Down
Loading