Skip to content

Commit 83cfc3b

Browse files
committed
Retry a REGEXP match without JIT when its stack runs out
PHP's JIT stack is small, so a repeated group like '(a|b)*' failed with "exceeded internal limits" on a subject of about 6,000 characters. MySQL matches such subjects, and trunk silently treated them as not matching. On PHP 7.3+, retry such a match with "(*NO_JIT)", which raises the limit to about 50,000 characters. PHP 7.2 has no "(*NO_JIT)", and matching without JIT can crash it, so it keeps the JIT limit of about 2,000 characters. Document the remaining limit.
1 parent 463f26b commit 83cfc3b

2 files changed

Lines changed: 74 additions & 14 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,46 @@ public function testRegexpBacktrackLimit() {
427427
);
428428
}
429429

430+
public function testRegexpJitStackLimit() {
431+
// A repeated group exhausts PHP's JIT stack after a few thousand characters.
432+
$subject = str_repeat( 'a', 20000 );
433+
if ( PHP_VERSION_ID < 70300 ) {
434+
// PHP 7.2 can't retry without JIT.
435+
$this->assertQueryError(
436+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
437+
'Regular expression evaluation exceeded internal limits.'
438+
);
439+
return;
440+
}
441+
$this->assertQuery(
442+
"SELECT
443+
'$subject' REGEXP '^(a|b)*\$' AS operator_result,
444+
REGEXP_LIKE('$subject', '^(a|b)*\$') AS like_result,
445+
LENGTH(REGEXP_SUBSTR('$subject', '(a|b)+')) AS substr_length,
446+
REGEXP_INSTR('$subject', '(a|b)+\$', 1, 1, 1) AS instr_result"
447+
);
448+
$this->assertEquals(
449+
array(
450+
(object) array(
451+
'operator_result' => '1',
452+
'like_result' => '1',
453+
'substr_length' => '20000',
454+
'instr_result' => '20001',
455+
),
456+
),
457+
$this->last_result
458+
);
459+
}
460+
461+
public function testRegexpLimitWithoutJit() {
462+
// Matching without JIT has a limit, too.
463+
$subject = str_repeat( 'a', 200000 );
464+
$this->assertQueryError(
465+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
466+
'Regular expression evaluation exceeded internal limits.'
467+
);
468+
}
469+
430470
public function testRegexpLegacyOperatorRegression() {
431471
// The legacy REGEXP operator must keep working alongside REGEXP_LIKE.
432472
$this->assertQuery( "SELECT 'abc' REGEXP 'ABC' AS r" );

0 commit comments

Comments
 (0)