azu / gitbook-plugin-include-codeblock

GitBook plugin for including file
Other
40 stars 25 forks source link

fix(parser): fix infinity loop #58

Closed azu closed 6 years ago

azu commented 6 years ago

fix #57

lwchkg commented 6 years ago

The page http://instanceof.me/post/52245507631/regex-emulate-atomic-grouping-with-lookahead appears to have a method to prevent "catastrophic backtracking", which is the cause of this bug.

lwchkg commented 6 years ago

Got the correct regex: /\[(?=((?:[^\]]|\\.)*))\1\]\(((?:[^\)]|\\.)*)\)/gm with the help of the page above. Idea: greedy capture in lookahead, then backreference to prevent backtracking. Here is the test: https://regex101.com/r/jnZU8P/1

Update: still wrong. :-( Now the (... part crashes.

lwchkg commented 6 years ago

So here is the finally correct one: /\[(?=((?:[^\]]|\\.)*))\1\]\((?=((?:[^\)]|\\.)*))\2\)/gm And the test: https://regex101.com/r/jnZU8P/2

Test string: `abc

[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890[\1\2\34567890](\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890(\1\2\34567890`