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
10 changes: 10 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ fn misspelled_character_class_is_error() {
}
}

#[test]
fn reversed_interval_is_error() {
for args in [&["-E", "fo{2,1}"][..], &["fo\\{2,1\\}"][..]] {
let (_s, mut c) = ucmd();
c.args(args)
.fails_with_code(2)
.stderr_contains("Invalid content of \\{\\}");
}
}

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