Skip to content

Commit

Permalink
Added a workaround for a PHP Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Oct 31, 2016
1 parent c30eb5e commit d759ed0
Show file tree
Hide file tree
Showing 6 changed files with 10,218 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,12 @@ CHANGELOG
Version 1.1.2 work in progress
------------------------------

...
- Added a workaround for a [PHP bug](https://bugs.php.net/bug.php?id=45735) which exists in versions `<` 7.0, where `preg_match()` causes a segfault
on [catastropic backtracking][] in emph/strong parsing.


[catastropic backtracking]: http://www.regular-expressions.info/catastrophic.html


Version 1.1.1 work in progress
------------------------------
Expand Down
16 changes: 16 additions & 0 deletions inline/EmphStrongTrait.php
Expand Up @@ -26,6 +26,14 @@ protected function parseEmphStrong($text)
}

if ($marker == $text[1]) { // strong
// work around a PHP bug that crashes with a segfault on too much regex backtrack
// check whether the end marker exists in the text
// https://github.com/erusev/parsedown/issues/443
// https://bugs.php.net/bug.php?id=45735
if (strpos($text, $marker . $marker, 2) === false) {
return [['text', $text[0] . $text[1]], 2];
}

if ($marker == '*' && preg_match('/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*]{2})/s', $text, $matches) ||
$marker == '_' && preg_match('/^__((?:[^_]|_[^_]*_)+?)__(?!__)/us', $text, $matches)) {

Expand All @@ -38,6 +46,14 @@ protected function parseEmphStrong($text)
];
}
} else { // emph
// work around a PHP bug that crashes with a segfault on too much regex backtrack
// check whether the end marker exists in the text
// https://github.com/erusev/parsedown/issues/443
// https://bugs.php.net/bug.php?id=45735
if (strpos($text, $marker, 1) === false) {
return [['text', $text[0]], 1];
}

if ($marker == '*' && preg_match('/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*][^*])/s', $text, $matches) ||
$marker == '_' && preg_match('/^_((?:[^_]|__[^_]*__)+?)_(?!_[^_])\b/us', $text, $matches)) {
return [
Expand Down

0 comments on commit d759ed0

Please sign in to comment.