@@ -614,11 +614,7 @@ public function regexp( $pattern, $field, $binary = 0 ) {
614614 */
615615 $ byte_mode = 1 === $ binary || ! $ this ->regexp_is_utf8 ( $ pattern ) || ! $ this ->regexp_is_utf8 ( $ field );
616616 $ compiled = $ this ->regexp_compile ( $ pattern , $ byte_mode ? 'c ' : '' , $ byte_mode );
617- $ result = preg_match ( $ compiled , $ field );
618- if ( false === $ result ) {
619- $ this ->regexp_fail ( $ pattern );
620- }
621- return $ result ;
617+ return $ this ->regexp_match ( $ compiled , $ pattern , $ field );
622618 }
623619
624620 /**
@@ -636,11 +632,7 @@ public function regexp_like( $expr, $pattern, $match_type = '' ) {
636632 return null ;
637633 }
638634 $ compiled = $ this ->regexp_compile ( $ pattern , $ match_type );
639- $ result = preg_match ( $ compiled , $ expr );
640- if ( false === $ result ) {
641- $ this ->regexp_fail ( $ pattern );
642- }
643- return $ result ;
635+ return $ this ->regexp_match ( $ compiled , $ pattern , $ expr );
644636 }
645637
646638 /**
@@ -1235,6 +1227,8 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12351227 * \p{Ll} on PCRE 10.44 and older. They stay case-sensitive in
12361228 * `(?i)[[:upper:]]` with the `c` flag, and in the whole pattern when it
12371229 * contains "(?-i)".
1230+ * - A repeated group, such as `(a|b)*`, can exceed PCRE's limits on a subject
1231+ * of about 50,000 characters, or 2,000 on PHP 7.2. MySQL allows more.
12381232 *
12391233 * @param string $pattern The MySQL regex pattern.
12401234 * @param string $match_type MySQL match_type flag string.
@@ -1579,16 +1573,14 @@ private function regexp_find_nth_match( $compiled, $pattern, $subject, $offset,
15791573 private function regexp_matches ( $ compiled , $ pattern , $ subject , $ offset ) {
15801574 $ len = strlen ( $ subject );
15811575 while ( $ offset <= $ len ) {
1582- $ r = preg_match (
1576+ $ r = $ this -> regexp_match (
15831577 $ compiled ,
1578+ $ pattern ,
15841579 $ subject ,
15851580 $ m ,
15861581 PREG_OFFSET_CAPTURE | PREG_UNMATCHED_AS_NULL ,
15871582 $ offset
15881583 );
1589- if ( false === $ r ) {
1590- $ this ->regexp_fail ( $ pattern );
1591- }
15921584 if ( 0 === $ r ) {
15931585 return ;
15941586 }
@@ -1608,6 +1600,34 @@ private function regexp_matches( $compiled, $pattern, $subject, $offset ) {
16081600 }
16091601 }
16101602
1603+ /**
1604+ * Run preg_match(), and report a failure as a MySQL error.
1605+ *
1606+ * PHP's JIT stack is small, so a repeated group, such as "(a|b)*", can fail on
1607+ * a subject of a few thousand characters. Matching without JIT goes further.
1608+ * PHP 7.2 has no "(*NO_JIT)", and matching without JIT can crash it.
1609+ *
1610+ * @param string $compiled PCRE-wrapped pattern.
1611+ * @param string $pattern The original MySQL regex pattern, for error messages.
1612+ * @param string $subject The subject string.
1613+ * @param array $matches Receives the match, like in preg_match().
1614+ * @param int $flags The preg_match() flags.
1615+ * @param int $offset Byte offset at which matching begins.
1616+ *
1617+ * @throws Exception On a preg failure.
1618+ * @return int 1 on match, 0 on no match.
1619+ */
1620+ private function regexp_match ( $ compiled , $ pattern , $ subject , &$ matches = null , $ flags = 0 , $ offset = 0 ) {
1621+ $ result = preg_match ( $ compiled , $ subject , $ matches , $ flags , $ offset );
1622+ if ( false === $ result && PREG_JIT_STACKLIMIT_ERROR === preg_last_error () && PHP_VERSION_ID >= 70300 ) {
1623+ $ result = preg_match ( '/(*NO_JIT) ' . substr ( $ compiled , 1 ), $ subject , $ matches , $ flags , $ offset );
1624+ }
1625+ if ( false === $ result ) {
1626+ $ this ->regexp_fail ( $ pattern );
1627+ }
1628+ return $ result ;
1629+ }
1630+
16111631 /**
16121632 * Translate a preg_* failure into a caller-friendly exception message.
16131633 *
0 commit comments