Skip to content

Commit

Permalink
Fortran: fix catastrophic backtracking
Browse files Browse the repository at this point in the history
\\\\|\\. is like \\. but causes catastrophic backtracking inside (...)* .
  • Loading branch information
jeanas committed Aug 14, 2022
1 parent 9bd072d commit d9278a5
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Version 2.13.0
* Coq: add some common keywords, improve recognition of ``Set``
and qualified identifiers (#2158)
* F*: Allow C-style comments anywhere in a line
* Fortran: fix catastrophic backtracking with backslashes in strings
(#2194)
* Go: add support for generics (#2167)
* Inform: update for version 6.40 (#2190)
* Isabelle: recognize cartouches (#2089)
Expand Down
4 changes: 2 additions & 2 deletions pygments/lexers/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class FortranLexer(RegexLexer):
],

'strings': [
(r'(?s)"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
(r"(?s)'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
(r'(?s)"(\\[0-7]+|\\[^0-7\n]|[^"\\])*"', String.Double),
(r"(?s)'(\\[0-7]+|\\[^0-7\n]|[^'\\])*'", String.Single),
],

'nums': [
Expand Down
112 changes: 112 additions & 0 deletions tests/snippets/fortran/test_string_cataback.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---input---
! Bad string, there isn't an even number of backslashes.
! This should not cause catastrophic backtracking.
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'

---tokens---
"! Bad string, there isn't an even number of backslashes.\n" Comment

'! This should not cause catastrophic backtracking.\n' Comment

"'" Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
'\\' Error
"'" Error
'\n' Text.Whitespace

0 comments on commit d9278a5

Please sign in to comment.