Skip to content

Commit

Permalink
JavaLexer: Demonstrate a catastrophic backtracking bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee committed Nov 9, 2020
1 parent afa9e3b commit d06e8a4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/test_java.py
Expand Up @@ -7,9 +7,11 @@
:license: BSD, see LICENSE for details.
"""

import time

import pytest

from pygments.token import Text, Name, Punctuation, Keyword, Number
from pygments.token import Keyword, Name, Number, Punctuation, String, Text
from pygments.lexers import JavaLexer


Expand Down Expand Up @@ -76,3 +78,24 @@ def test_numeric_literals(lexer):
(Text, '\n')
]
assert list(lexer.get_tokens(fragment)) == tokens


@pytest.mark.parametrize(
'text',
(
'""', '"abc"', '"ひらがな"', '"123"',
'"\\\\"', '"\\t"' '"\\""',
),
)
def test_string_literals_positive_match(lexer, text):
"""Test positive matches for string literals."""
tokens = list(lexer.get_tokens_unprocessed(text))
assert all([token is String for _, token, _ in tokens])
assert ''.join([value for _, _, value in tokens]) == text


def test_string_literals_backtracking(lexer):
"""Test catastrophic backtracking for string literals."""
start_time = time.time()
list(lexer.get_tokens_unprocessed('"' + '\\' * 100))
assert time.time() - start_time < 1, 'possible backtracking bug'

0 comments on commit d06e8a4

Please sign in to comment.