Skip to content

Commit b7376a2

Browse files
committed
Avoid overflowing the stack with a lot of nested blocks.
1 parent 52f2d41 commit b7376a2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/parser.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,15 +691,22 @@ impl<'i, 't> Parser<'i, 't> {
691691
}
692692

693693

694-
/// Return value indicates whether the end of the input was reached.
695694
fn consume_until_end_of_block(block_type: BlockType, tokenizer: &mut Tokenizer) {
695+
let mut stack = vec![block_type];
696+
696697
// FIXME: have a special-purpose tokenizer method for this that does less work.
697698
while let Ok(ref token) = tokenizer.next() {
698-
if BlockType::closing(token) == Some(block_type) {
699-
return
699+
if let Some(b) = BlockType::closing(token) {
700+
if *stack.last().unwrap() == b {
701+
stack.pop();
702+
if stack.is_empty() {
703+
return;
704+
}
705+
}
700706
}
707+
701708
if let Some(block_type) = BlockType::opening(token) {
702-
consume_until_end_of_block(block_type, tokenizer);
709+
stack.push(block_type);
703710
}
704711
}
705712
}

0 commit comments

Comments
 (0)