azu / gitbook-plugin-include-codeblock

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

Infinite loop with a specific markdown file #57

Closed jeanregisser closed 6 years ago

jeanregisser commented 6 years ago

Hi,

I was getting build failure on Gitbook as seen in https://github.com/GitbookIO/feedback/issues/548#issuecomment-326613412

I tracked it down to the following markdown https://github.com/jeanregisser/mediaremotetv-protocol/blob/d0f862f946ccb9519c96ef7ee6f1c31460a76b96/pairing/README.md

When include-codeblock is enabled, this file makes gitbook serve use 100% CPU and never actually start serving.

If I remove line 35 and 55: https://github.com/jeanregisser/mediaremotetv-protocol/blame/d0f862f946ccb9519c96ef7ee6f1c31460a76b96/pairing/README.md#L35 https://github.com/jeanregisser/mediaremotetv-protocol/blame/d0f862f946ccb9519c96ef7ee6f1c31460a76b96/pairing/README.md#L55

It works again.

This was working fine with gitbook-plugin-include-codeblock@1.9.1 and now failing with gitbook-plugin-include-codeblock@3.1.3.

Wondering if it might be caused by https://github.com/azu/gitbook-plugin-include-codeblock/commit/e1866d9a2842728aebeeadac76c8cc1fbbfad764 as those lines do include some [ chars.

Let me know if you need more info.

Thanks,

jeanregisser commented 6 years ago

Confirmed e1866d9 is causing the issue.

gitbook-plugin-include-codeblock@3.1.0 works fine. gitbook-plugin-include-codeblock@3.1.1 fails as explained above.

azu commented 6 years ago

@jeanregisser Thanks for report. I will investigate it.

https://github.com/azu/gitbook-plugin-include-codeblock/commit/e1866d9a2842728aebeeadac76c8cc1fbbfad764#diff-6947033678b93d106e25614dd972e66fR22 https://github.com/azu/gitbook-plugin-include-codeblock/commit/e1866d9a2842728aebeeadac76c8cc1fbbfad764#diff-6947033678b93d106e25614dd972e66fR136

Maybe, This lines are problem. RegExp pattern it-self is issue.

Because

If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property). -- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Finding_successive_matches

/cc @lwchkg

azu commented 6 years ago

I've confirm this bug that is caused by following RegExp.

Infinity loop:

const markdownLinkFormatRegExp = /\[((?:[^\]]|\\.)*?)]\(((?:[^)]|\\.)*?)\)/g;

OK:

const markdownLinkFormatRegExp = /\[([^\]]*?)\]\(([^\)]*?)\)/gm;
azu commented 6 years ago

@jeanregisser We have released https://github.com/azu/gitbook-plugin-include-codeblock/releases/tag/v3.1.4 Please confirm it.

jeanregisser commented 6 years ago

3.1.4 looks all good now! Thanks a lot @azu!