Skip to content

Commit 5f3ddfe

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 28e562d commit 5f3ddfe

2 files changed

Lines changed: 65 additions & 10 deletions

File tree

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,7 @@ public function regexp( $pattern, $field, $binary = 0 ) {
610610
*/
611611
$byte_mode = 1 === $binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field );
612612
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode );
613-
$result = preg_match( $compiled, $field );
614-
if ( false === $result ) {
615-
$this->regexp_fail( $pattern );
616-
}
617-
return $result;
613+
return $this->regexp_match( $compiled, $pattern, $field );
618614
}
619615

620616
/**
@@ -634,11 +630,7 @@ public function regexp_like( $expr, $pattern, $match_type = '' ) {
634630
return null;
635631
}
636632
$compiled = $this->regexp_compile( $pattern, $match_type );
637-
$result = preg_match( $compiled, $expr );
638-
if ( false === $result ) {
639-
$this->regexp_fail( $pattern );
640-
}
641-
return $result;
633+
return $this->regexp_match( $compiled, $pattern, $expr );
642634
}
643635

644636
/**
@@ -1078,6 +1070,8 @@ private function regexp_check_match_type( $function_name, $match_type ) {
10781070
* - SQLite converts float and decimal operands to text, in a different format
10791071
* from MySQL: `20e0` reads "20.0" (MySQL: "20"), `0.000001` reads "1.0e-06",
10801072
* and `1.50` reads "1.5". Integers above the BIGINT range read as floats.
1073+
* - A repeated group, such as `(a|b)*`, can exceed PCRE's limits on a subject
1074+
* of about 50,000 characters, or 2,000 on PHP 7.2. MySQL allows more.
10811075
*
10821076
* @param string $pattern The MySQL regex pattern.
10831077
* @param string $match_type MySQL match_type flag string.
@@ -1209,6 +1203,31 @@ private function regexp_is_utf8( $value ) {
12091203
return 1 === preg_match( '//u', $value );
12101204
}
12111205

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

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,42 @@ public function testRegexpBacktrackLimit() {
363363
);
364364
}
365365

366+
public function testRegexpJitStackLimit() {
367+
// A repeated group exhausts PHP's JIT stack after a few thousand characters.
368+
$subject = str_repeat( 'a', 20000 );
369+
if ( PHP_VERSION_ID < 70300 ) {
370+
// PHP 7.2 can't retry without JIT.
371+
$this->assertQueryError(
372+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
373+
'Regular expression evaluation exceeded internal limits.'
374+
);
375+
return;
376+
}
377+
$this->assertQuery(
378+
"SELECT
379+
'$subject' REGEXP '^(a|b)*\$' AS operator_result,
380+
REGEXP_LIKE('$subject', '^(a|b)*\$') AS like_result"
381+
);
382+
$this->assertEquals(
383+
array(
384+
(object) array(
385+
'operator_result' => '1',
386+
'like_result' => '1',
387+
),
388+
),
389+
$this->last_result
390+
);
391+
}
392+
393+
public function testRegexpLimitWithoutJit() {
394+
// Matching without JIT has a limit, too.
395+
$subject = str_repeat( 'a', 200000 );
396+
$this->assertQueryError(
397+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
398+
'Regular expression evaluation exceeded internal limits.'
399+
);
400+
}
401+
366402
public function testRegexpLegacyOperatorRegression() {
367403
// The legacy REGEXP operator must keep working alongside REGEXP_LIKE.
368404
$this->assertQuery( "SELECT 'abc' REGEXP 'ABC' AS r" );

0 commit comments

Comments
 (0)