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

preg_match in inlineLink() causes segfault #352

Open
clphillips opened this issue Sep 29, 2015 · 4 comments
Open

preg_match in inlineLink() causes segfault #352

clphillips opened this issue Sep 29, 2015 · 4 comments
Labels

Comments

@clphillips
Copy link

Here's the culprit:

preg_match('/\[((?:[^][]|(?R))*)\]/', $remainder, $matches)

The following $EmRegex also suffers from this problem:

/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us

These regular expressions trigger segfaults on an 11 KB input string of mostly JSON text (~80 chars per line).

See PHP issue #45735.

I believe the regexes could be improved to prevent segfault.

@ifeltsweet
Copy link

+1, happens to us often on large documents.

@perryflynn
Copy link

@erusev erusev added the bug label Oct 12, 2015
@CasimirEtHippolyte
Copy link

CasimirEtHippolyte commented May 23, 2016

To prevent backtracking limit to be reached and to fail faster, the two patterns can be written like this:

preg_match('/\[([^][]*+(?:(?R)[^][]*)*+)]/', $remainder, $matches)

/^_([^\\\\_]*+(?:(?:\\\\.|__[^_]*__)[^\\\\_]*)*+_(?!_)/s

( (?!_) looked redundant with the word-boundary, but you can always replace (?!_) with \b if it's the behaviour you are looking for. In this case don't forget to add the u modifier, but if you keep (?!_) the u modifier is useless.)

@erusev
Copy link
Owner

erusev commented May 24, 2016

Thanks @CasimirEtHippolyte, I'll review these as soon as I have time.

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

No branches or pull requests

5 participants