Skip to content

Commit 28e562d

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 9975e2c commit 28e562d

3 files changed

Lines changed: 178 additions & 206 deletions

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
@@ -1056,6 +1056,7 @@ private function regexp_check_match_type( $function_name, $match_type ) {
10561056
* - ICU supports multi-code-point case folds such as "ß" matching "ss";
10571057
* PCRE's case-insensitive mode does not.
10581058
* - ICU-only syntax, such as `\uXXXX` and `\N{name}` in patterns, is an error.
1059+
* - An unknown escape, such as `\y`, is an error. ICU reads it as a literal.
10591060
* - ICU reads CRLF as one line break, and PCRE as two characters. `$` also
10601061
* matches between them, and `.` with the `n` flag matches only one of them.
10611062
* - ICU's nested sets and set operations don't exist in PCRE. `[a[b]]` and
@@ -1116,7 +1117,11 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
11161117
}
11171118
}
11181119

1119-
$modifiers = $binary_mode ? '' : 'u';
1120+
/*
1121+
* PHP 7 can read an unknown escape, such as "\y" or "\u0041", as literal text.
1122+
* The "X" modifier makes it an error, like in PHP 8.
1123+
*/
1124+
$modifiers = $binary_mode ? 'X' : 'uX';
11201125
if ( ! $case_sensitive ) {
11211126
$modifiers .= 'i';
11221127
}

‎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
@@ -324,6 +324,11 @@ public function testRegexpLikeErrors( $sql, $message ) {
324324
public static function regexpLikeErrors() {
325325
return array(
326326
'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ),
327+
'unknown escape' => array( "REGEXP_LIKE('y', '\\\\y')", 'Invalid regular expression: \\y.' ),
328+
'ICU escape' => array( "REGEXP_LIKE('A', '\\\\u0041')", 'Invalid regular expression: \\u0041.' ),
329+
'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ),
330+
'operator escape' => array( "'y' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
331+
'binary escape' => array( "X'FF' REGEXP '\\\\y'", 'Invalid regular expression: \\y.' ),
327332
'extra argument' => array(
328333
"REGEXP_LIKE('abc', 'abc', 'c', 'extra')",
329334
'SQLSTATE[HY000]: General error: 1 wrong number of arguments to function REGEXP_LIKE()',

0 commit comments

Comments
 (0)