Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Prevent Regex::new() from panicking when a non-AST item is repeated
This bug has also affected the regex! macro, which has caused an ICE
when such an invalid expression is provided.

Fixes #20208.
  • Loading branch information
barosl committed Dec 26, 2014
1 parent 5ba6102 commit 0bd8534
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/libregex/parse.rs
Expand Up @@ -320,9 +320,10 @@ impl<'a> Parser<'a> {
}

fn push_repeater(&mut self, c: char) -> Result<(), Error> {
if self.stack.len() == 0 {
return self.err(
"A repeat operator must be preceded by a valid expression.")
match self.stack.last() {
Some(&Expr(..)) => (),
// self.stack is empty, or the top item is not an Expr
_ => return self.err("A repeat operator must be preceded by a valid expression."),
}
let rep: Repeater = match c {
'?' => ZeroOne, '*' => ZeroMore, '+' => OneMore,
Expand Down
1 change: 1 addition & 0 deletions src/libregex/test/tests.rs
Expand Up @@ -142,6 +142,7 @@ noparse!{fail_range_end_no_class, "[a-[:lower:]]"}
noparse!{fail_range_end_no_begin, r"[a-\A]"}
noparse!{fail_range_end_no_end, r"[a-\z]"}
noparse!{fail_range_end_no_boundary, r"[a-\b]"}
noparse!{fail_repeat_no_expr, r"-|+"}

macro_rules! mat {
($name:ident, $re:expr, $text:expr, $($loc:tt)+) => (
Expand Down

7 comments on commit 0bd8534

@bors
Copy link
Contributor

@bors bors commented on 0bd8534 Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at barosl@0bd8534

@bors
Copy link
Contributor

@bors bors commented on 0bd8534 Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging barosl/rust/regex-repeater-panic = 0bd8534 into auto

@bors
Copy link
Contributor

@bors bors commented on 0bd8534 Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "9be54be15b4b4ba5c1b22d958d7619a5154ab469"}

@bors
Copy link
Contributor

@bors bors commented on 0bd8534 Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

barosl/rust/regex-repeater-panic = 0bd8534 merged ok, testing candidate = 9be54be

@bors
Copy link
Contributor

@bors bors commented on 0bd8534 Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 9be54be

@bors
Copy link
Contributor

@bors bors commented on 0bd8534 Dec 27, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 9be54be

Please sign in to comment.