Skip to content

Commit

Permalink
Improve worst-case performance of inline.text regex
Browse files Browse the repository at this point in the history
The old regex may take quadratic time to scan for potential email
addresses starting at every point.  Fix it to avoid scanning from
points that would have been in the middle of a previous scan.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed Apr 3, 2019
1 parent ba1de1e commit 175fae6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
5 changes: 1 addition & 4 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,7 @@ inline.gfm = merge({}, inline.normal, {
url: /^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,
_backpedal: /(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,
del: /^~+(?=\S)([\s\S]*?\S)~+/,
text: edit(inline.text)
.replace(']|', '~]|')
.replace('|$', '|https?://|ftp://|www\\.|[a-zA-Z0-9.!#$%&\'*+/=?^_`{\\|}~-]+@|$')
.getRegex()
text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*~]|\b_| {2,}\n|https?:\/\/|ftp:\/\/|www\.|$)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/
});

inline.gfm.url = edit(inline.gfm.url, 'i')
Expand Down
3 changes: 1 addition & 2 deletions test/specs/gfm/gfm.0.28.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@
"section": "Autolinks",
"html": "<p><a href=\"mailto:a.b-c_d@a.b\">a.b-c_d@a.b</a></p>\n<p><a href=\"mailto:a.b-c_d@a.b\">a.b-c_d@a.b</a>.</p>\n<p>a.b-c_d@a.b-</p>\n<p>a.b-c_d@a.b_</p>",
"markdown": "a.b-c_d@a.b\n\na.b-c_d@a.b.\n\na.b-c_d@a.b-\n\na.b-c_d@a.b_",
"example": 607,
"shouldFail": true
"example": 607
},
{
"section": "Disallowed Raw HTML",
Expand Down

0 comments on commit 175fae6

Please sign in to comment.