Skip to content

Commit 2b6f674

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. The "X" modifier makes them errors on PHP 7, too, and does nothing on PHP 8. MySQL reads escapes like '\y' as literals, so PHP 7 was right for those. Now PHP 7 fails the same REGEXP_LIKE() cases as PHP 8.
1 parent 226e10f commit 2b6f674

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
@@ -1099,7 +1099,12 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
10991099
}
11001100
}
11011101

1102-
$modifiers = $binary_mode ? '' : 'u';
1102+
/*
1103+
* PHP 7 can read an unknown escape as literal text. For "\y", that matches ICU,
1104+
* but "\u0041" matches "u0041", where ICU matches "A". The "X" modifier makes
1105+
* all of them errors, like in PHP 8.
1106+
*/
1107+
$modifiers = $binary_mode ? 'X' : 'uX';
11031108
if ( ! $case_sensitive ) {
11041109
$modifiers .= 'i';
11051110
}

‎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)