Skip to content

Commit 463f26b

Browse files
committed
Reject unknown escapes in REGEXP patterns on PHP 7
PHP 7.3 and 7.4 read unknown escapes as literal text. '\u0041' silently matched "u0041", and '\p{Foo}' matched nothing instead of failing. PHP 7.2 did the same for escapes like '\y'. PHP 8 reports them as errors, as the known differences say. The "X" modifier makes them errors on PHP 7, too, and does nothing on PHP 8. MySQL reads unknown escapes like '\y' as literals, so document that difference as well.
1 parent 7ed6ba1 commit 463f26b

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ public function _helper_like_to_glob_pattern( $pattern ) {
12051205
* - ICU supports multi-code-point case folds such as "ß" matching "ss";
12061206
* PCRE's case-insensitive mode does not.
12071207
* - ICU-only syntax, such as `\uXXXX` and `\N{name}` in patterns, is an error.
1208+
* - An unknown escape, such as `\y`, is an error. ICU reads it as a literal.
12081209
* - ICU reads CRLF as one line break, and PCRE as two characters. `$` also
12091210
* matches between them, and `.` with the `n` flag matches only one of them.
12101211
* - ICU's nested sets and set operations don't exist in PCRE. `[a[b]]` and
@@ -1276,7 +1277,11 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
12761277
}
12771278
}
12781279

1279-
$modifiers = $binary_mode ? '' : 'u';
1280+
/*
1281+
* PHP 7 can read an unknown escape, such as "\y" or "\u0041", as literal text.
1282+
* The "X" modifier makes it an error, like in PHP 8.
1283+
*/
1284+
$modifiers = $binary_mode ? 'X' : 'uX';
12801285
if ( ! $case_sensitive ) {
12811286
$modifiers .= 'i';
12821287
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ public static function regexpLikeErrors() {
388388
return array(
389389
'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ),
390390
'invalid flag' => array( "REGEXP_LIKE('abc', 'a', 'x')", 'Invalid match_type flag: x.' ),
391+
'unknown escape' => array( "REGEXP_LIKE('y', '\\\\y')", 'Invalid regular expression: \\y.' ),
392+
'ICU escape' => array( "REGEXP_LIKE('A', '\\\\u0041')", 'Invalid regular expression: \\u0041.' ),
393+
'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ),
394+
'operator escape' => array( "'y' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
395+
'binary escape' => array( "X'FF' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
391396
'extra argument' => array(
392397
"REGEXP_LIKE('abc', 'abc', 'c', 'extra')",
393398
'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_LIKE()',

0 commit comments

Comments
 (0)