Skip to content

Commit dccda03

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 d7183e1 commit dccda03

2 files changed

Lines changed: 74 additions & 10 deletions

File tree

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,7 @@ public function regexp( $pattern, $field, $binary = 0 ) {
607607
*/
608608
$byte_mode = 1 === $binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field );
609609
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode );
610-
$result = preg_match( $compiled, $field );
611-
if ( false === $result ) {
612-
$this->regexp_fail( $pattern );
613-
}
614-
return $result;
610+
return $this->regexp_match( $compiled, $pattern, $field );
615611
}
616612

617613
/**
@@ -635,11 +631,7 @@ public function regexp_like( $expr, $pattern, $match_type = '' ) {
635631
// and case-sensitively, unless the match type says otherwise.
636632
$byte_mode = ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $expr );
637633
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' . $match_type : $match_type, $byte_mode );
638-
$result = preg_match( $compiled, $expr );
639-
if ( false === $result ) {
640-
$this->regexp_fail( $pattern );
641-
}
642-
return $result;
634+
return $this->regexp_match( $compiled, $pattern, $expr );
643635
}
644636

645637
/**
@@ -1061,6 +1053,8 @@ private function regexp_check_match_type( $function_name, $match_type ) {
10611053
* sensitivity can differ between rows.
10621054
* - SQLite converts numbers to text in its own format, so `20e0` reads "20.0"
10631055
* (MySQL: "20"), and `0.000001` reads "1.0e-06".
1056+
* - A repeated group, like `(a|b)*`, can exceed PCRE's limits on a subject of
1057+
* about 50,000 characters, or 2,000 on PHP 7.2.
10641058
*
10651059
* @param string $pattern The MySQL regex pattern.
10661060
* @param string $match_type MySQL match_type flag string.
@@ -1214,6 +1208,33 @@ private function regexp_is_utf8( $value ) {
12141208
return 1 === preg_match( '//u', $value );
12151209
}
12161210

1211+
/**
1212+
* Run preg_match(), and report a failure as a MySQL error.
1213+
*
1214+
* @param string $compiled PCRE-wrapped pattern.
1215+
* @param string $pattern The original MySQL regex pattern, for error messages.
1216+
* @param string $subject The subject string.
1217+
*
1218+
* @throws Exception On a preg failure.
1219+
* @return int 1 on match, 0 on no match.
1220+
*/
1221+
private function regexp_match( $compiled, $pattern, $subject ) {
1222+
$result = preg_match( $compiled, $subject );
1223+
1224+
/*
1225+
* PHP's JIT stack is small, so a repeated group, such as "(a|b)*", can fail on
1226+
* a subject of a few thousand characters. Matching without JIT goes further.
1227+
* PHP 7.2 has no "(*NO_JIT)", and matching without JIT can crash it.
1228+
*/
1229+
if ( false === $result && PREG_JIT_STACKLIMIT_ERROR === preg_last_error() && PHP_VERSION_ID >= 70300 ) {
1230+
$result = preg_match( $compiled[0] . '(*NO_JIT)' . substr( $compiled, 1 ), $subject );
1231+
}
1232+
if ( false === $result ) {
1233+
$this->regexp_fail( $pattern );
1234+
}
1235+
return $result;
1236+
}
1237+
12171238
/**
12181239
* Translate a preg_* failure into a caller-friendly exception message.
12191240
*

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,49 @@ public function testRegexpLimitOnlyOnEmptySubject() {
411411
);
412412
}
413413

414+
public function testRegexpJitStackLimit() {
415+
// A repeated group exhausts PHP's JIT stack after a few thousand characters.
416+
$subject = str_repeat( 'a', 20000 );
417+
if ( PHP_VERSION_ID < 70300 ) {
418+
// PHP 7.2 can't retry without JIT. With JIT off, its PCRE 8 recurses on
419+
// the C stack, and the default limit of 100,000 overflows the stack.
420+
$this->iniSet( 'pcre.recursion_limit', '1000' );
421+
$this->assertQueryError(
422+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
423+
'Regular expression evaluation exceeded internal limits.'
424+
);
425+
return;
426+
}
427+
$this->assertQuery(
428+
"SELECT
429+
'$subject' REGEXP '^(a|b)*\$' AS operator_result,
430+
REGEXP_LIKE('$subject', '^(a|b)*\$') AS like_result"
431+
);
432+
$this->assertEquals(
433+
array(
434+
(object) array(
435+
'operator_result' => '1',
436+
'like_result' => '1',
437+
),
438+
),
439+
$this->last_result
440+
);
441+
}
442+
443+
public function testRegexpLimitWithoutJit() {
444+
// Matching without JIT has a limit, too.
445+
if ( PHP_VERSION_ID < 70300 ) {
446+
// With JIT off, PHP 7.2's PCRE 8 recurses on the C stack, and the
447+
// default limit of 100,000 overflows the stack.
448+
$this->iniSet( 'pcre.recursion_limit', '1000' );
449+
}
450+
$subject = str_repeat( 'a', 200000 );
451+
$this->assertQueryError(
452+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
453+
'Regular expression evaluation exceeded internal limits.'
454+
);
455+
}
456+
414457
public function testRegexpLegacyOperatorRegression() {
415458
// The legacy REGEXP operator must keep working alongside REGEXP_LIKE.
416459
$this->assertQuery( "SELECT 'abc' REGEXP 'ABC' AS r" );

0 commit comments

Comments
 (0)