Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing '[ (]([ (]([ (]([ (](...' takes quadratic time #285

Closed
mity opened this issue Mar 11, 2019 · 3 comments
Closed

Parsing '[ (]([ (]([ (]([ (](...' takes quadratic time #285

mity opened this issue Mar 11, 2019 · 3 comments

Comments

@mity
Copy link
Contributor

mity commented Mar 11, 2019

$ time py -c 'print("[ (](" * 10000)' | ./cmark >/dev/null
real    0m0.232s
user    0m0.030s
sys     0m0.000s

$ time py -c 'print("[ (](" * 40000)' | ./cmark >/dev/null
real    0m2.168s
user    0m0.000s
sys     0m0.045s

$ time py -c 'print("[ (](" * 80000)' | ./cmark >/dev/null
real    0m8.278s
user    0m0.016s
sys     0m0.046s

(Originally reported by @andersk for MD4C in mity/md4c#57 but applies to Cmark too.)

@mity
Copy link
Contributor Author

mity commented Mar 11, 2019

In MD4C, it was fixed by mity/md4c@966b8e3.

See the specs:

A link title consists of either

  • a sequence of zero or more characters between straight double-quote characters ("), including a " character only if it is backslash-escaped, or
  • a sequence of zero or more characters between straight single-quote characters ('), including a ' character only if it is backslash-escaped, or
  • a sequence of zero or more characters between matching parentheses ((...)), including a ) character only if it is backslash-escaped.

Imho the 3rd option has to be updated to disallow also (. Otherwise, given this test case, you have to scan till the end of the input for every part of the input pattern, leading to O(n^2). Furthermore allowing it leads to very unreadable and unintuitive behavior:

$ echo '[x](url (crazy_title())' | ./cmark
<p><a href="url" title="crazy_title(">x</a></p>

@mity
Copy link
Contributor Author

mity commented Mar 12, 2019

This was also already touched here: commonmark/commonmark-spec#526

@jgm
Copy link
Member

jgm commented Mar 17, 2019

Fixed by 9f7d0a6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants