Skip to content

Commit ec0722a

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 d5112a5 commit ec0722a

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.
@@ -1213,6 +1207,33 @@ private function regexp_is_utf8( $value ) {
12131207
return 1 === preg_match( '//u', $value );
12141208
}
12151209

1210+
/**
1211+
* Run preg_match(), and report a failure as a MySQL error.
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+
1223+
/*
1224+
* PHP's JIT stack is small, so a repeated group, such as "(a|b)*", can fail on
1225+
* a subject of a few thousand characters. Matching without JIT goes further.
1226+
* PHP 7.2 has no "(*NO_JIT)", and matching without JIT can crash it.
1227+
*/
1228+
if ( false === $result && PREG_JIT_STACKLIMIT_ERROR === preg_last_error() && PHP_VERSION_ID >= 70300 ) {
1229+
$result = preg_match( $compiled[0] . '(*NO_JIT)' . substr( $compiled, 1 ), $subject );
1230+
}
1231+
if ( false === $result ) {
1232+
$this->regexp_fail( $pattern );
1233+
}
1234+
return $result;
1235+
}
1236+
12161237
/**
12171238
* Translate a preg_* failure into a caller-friendly exception message.
12181239
*

‎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
@@ -390,6 +390,49 @@ public function testRegexpBacktrackLimit() {
390390
);
391391
}
392392

393+
public function testRegexpJitStackLimit() {
394+
// A repeated group exhausts PHP's JIT stack after a few thousand characters.
395+
$subject = str_repeat( 'a', 20000 );
396+
if ( PHP_VERSION_ID < 70300 ) {
397+
// PHP 7.2 can't retry without JIT. With JIT off, its PCRE 8 recurses on
398+
// the C stack, and the default limit of 100,000 overflows the stack.
399+
$this->iniSet( 'pcre.recursion_limit', '1000' );
400+
$this->assertQueryError(
401+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
402+
'Regular expression evaluation exceeded internal limits.'
403+
);
404+
return;
405+
}
406+
$this->assertQuery(
407+
"SELECT
408+
'$subject' REGEXP '^(a|b)*\$' AS operator_result,
409+
REGEXP_LIKE('$subject', '^(a|b)*\$') AS like_result"
410+
);
411+
$this->assertEquals(
412+
array(
413+
(object) array(
414+
'operator_result' => '1',
415+
'like_result' => '1',
416+
),
417+
),
418+
$this->last_result
419+
);
420+
}
421+
422+
public function testRegexpLimitWithoutJit() {
423+
// Matching without JIT has a limit, too.
424+
if ( PHP_VERSION_ID < 70300 ) {
425+
// With JIT off, PHP 7.2's PCRE 8 recurses on the C stack, and the
426+
// default limit of 100,000 overflows the stack.
427+
$this->iniSet( 'pcre.recursion_limit', '1000' );
428+
}
429+
$subject = str_repeat( 'a', 200000 );
430+
$this->assertQueryError(
431+
"SELECT REGEXP_LIKE('$subject', '^(a|b)*\$')",
432+
'Regular expression evaluation exceeded internal limits.'
433+
);
434+
}
435+
393436
public function testRegexpLegacyOperatorRegression() {
394437
// The legacy REGEXP operator must keep working alongside REGEXP_LIKE.
395438
$this->assertQuery( "SELECT 'abc' REGEXP 'ABC' AS r" );

0 commit comments

Comments
 (0)