Skip to content

Commit

Permalink
syntax: forbid \P{any}
Browse files Browse the repository at this point in the history
Previously, the translator would forbid constructs like [^\w\W] that
compiled to empty character classes. These things are forbidden not
because the translator can't handle it, but because the compile in
'regex' proper can't handle it. Once we migrate to the compiler in
regex-automata, which supports empty classes, then we can lift this
restriction. But until then, we should ban all such instances. It turns
out that \P{any} was another way to utter this, so we ban it in this
commit.

This was found by OSS-Fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26505

Fixes #722
  • Loading branch information
BurntSushi committed Nov 1, 2020
1 parent a7ef5f4 commit 6fdb6e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 22 additions & 2 deletions regex-syntax/src/hir/translate.rs
Expand Up @@ -322,7 +322,7 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
ast.negated,
&mut cls,
)?;
if cls.iter().next().is_none() {
if cls.ranges().is_empty() {
return Err(self.error(
ast.span,
ErrorKind::EmptyClassNotAllowed,
Expand All @@ -337,7 +337,7 @@ impl<'t, 'p> Visitor for TranslatorI<'t, 'p> {
ast.negated,
&mut cls,
)?;
if cls.iter().next().is_none() {
if cls.ranges().is_empty() {
return Err(self.error(
ast.span,
ErrorKind::EmptyClassNotAllowed,
Expand Down Expand Up @@ -844,6 +844,11 @@ impl<'t, 'p> TranslatorI<'t, 'p> {
ast_class.negated,
class,
)?;
if class.ranges().is_empty() {
let err = self
.error(ast_class.span, ErrorKind::EmptyClassNotAllowed);
return Err(err);
}
}
result
}
Expand Down Expand Up @@ -2317,6 +2322,21 @@ mod tests {
);
}

#[test]
#[cfg(feature = "unicode-gencat")]
fn class_unicode_any_empty() {
assert_eq!(
t_err(r"\P{any}"),
TestError {
kind: hir::ErrorKind::EmptyClassNotAllowed,
span: Span::new(
Position::new(0, 1, 1),
Position::new(7, 1, 8)
),
}
);
}

#[test]
#[cfg(not(feature = "unicode-age"))]
fn class_unicode_age_disabled() {
Expand Down
7 changes: 7 additions & 0 deletions tests/regression_fuzz.rs
Expand Up @@ -10,3 +10,10 @@
fn fuzz1() {
regex!(r"1}{55}{0}*{1}{55}{55}{5}*{1}{55}+{56}|;**");
}

// See: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26505
// See: https://github.com/rust-lang/regex/issues/722
#[test]
fn empty_any_errors_no_panic() {
assert!(regex_new!(r"\P{any}").is_err());
}

0 comments on commit 6fdb6e1

Please sign in to comment.