Skip to content

Commit

Permalink
Avoid infinite loop in lexer on unclosed code block
Browse files Browse the repository at this point in the history
An unclosed code block will cause the lexer to loop infinitely in the
'block' rule, because 'eof' is part of the 'eol' rule and it'll keep
matching the last rule.

Add a separate rule for eof that terminates immediately,
the way the first rule was written it would expect closing quotes after
eof which would never match eof.

Signed-off-by: Edwin Török <edwin@etorok.net>
  • Loading branch information
edwintorok committed Jan 17, 2024
1 parent 3226c6e commit 9a8facd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/lexer_mdx.mll
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ rule text section = parse
`Text str :: text section lexbuf }

and block = parse
| eof | ws* as end_pad "```" ws* eol
| eof { [""] }
| ws* as end_pad "```" ws* eol
{ newline lexbuf;
[end_pad] }
| ([^'\n']* as str) eol
Expand Down
12 changes: 12 additions & 0 deletions test/bin/mdx-test/expect/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,18 @@
(alias runtest)
(action (diff trailing-whitespaces/test-case.md.expected trailing-whitespaces.actual)))

(rule
(target unclosed-block.actual)
(deps (package mdx) (source_tree unclosed-block))
(action
(with-stdout-to %{target}
(chdir unclosed-block
(run ocaml-mdx test --output - test-case.md)))))

(rule
(alias runtest)
(action (diff unclosed-block/test-case.md.expected unclosed-block.actual)))

(rule
(target warnings.actual)
(deps (package mdx) (source_tree warnings))
Expand Down
7 changes: 7 additions & 0 deletions test/bin/mdx-test/expect/unclosed-block/test-case.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Here is a good block:

```ocaml
```

And here is one that is not closed, this should not cause an infinite loop in the lexer:
```
8 changes: 8 additions & 0 deletions test/bin/mdx-test/expect/unclosed-block/test-case.md.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Here is a good block:

```ocaml
```

And here is one that is not closed, this should not cause an infinite loop in the lexer:
```
```

0 comments on commit 9a8facd

Please sign in to comment.