From b11e4d4dac29e0d6e381d177c9d475298db65295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Thu, 3 Dec 2026 14:02:33 +0100 Subject: [PATCH 1/2] Accept shorthand hyphens in simple REGEXP sets MySQL accepts a literal hyphen after a shorthand class in email and slug patterns. PCRE2 rejects it as an invalid range. Escape the hyphen when all sets use simple literals, ranges or escapes. Outside sets, allow simple escapes, counted repeats, and ordinary or noncapturing groups. A raw #, other group syntax, backreferences, Unicode properties, and hex, octal or named escapes disable the rewrite. Keep PCRE's existing Unicode shorthand definitions. For example, ICU's \w includes join controls and excludes superscript digits. PCRE differs on both. PCRE also includes U+180E in horizontal whitespace, while ICU excludes it. This resolves 78 known failures in each MySQL-judged REGEXP suite. --- ...s-wp-sqlite-pdo-user-defined-functions.php | 32 ++++- .../tests/WP_MySQL_On_SQLite_Tests.php | 111 ++++++++++++++++++ .../WP_MySQL_On_SQLite_Regexp_Instr_Tests.php | 82 +------------ .../WP_MySQL_On_SQLite_Regexp_Like_Tests.php | 82 +------------ ...P_MySQL_On_SQLite_Regexp_Replace_Tests.php | 82 +------------ ...WP_MySQL_On_SQLite_Regexp_Substr_Tests.php | 82 +------------ 6 files changed, 150 insertions(+), 321 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 91b40dea6..8d290d616 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -1354,7 +1354,8 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w /** * Escape pattern delimiters and NUL bytes that are not already escaped, read - * "\K" as "K", and, if asked, make "\G" never match. + * "\K" as "K", escape a hyphen after a shorthand class, and, if asked, make + * "\G" never match. * * PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00". * @@ -1380,6 +1381,30 @@ private function regexp_escape_pattern( $pattern, $without_g = false ) { $escapes['G'] = '\\b\\B'; } + /* + * ICU reads the hyphen in "[\w-.]" literally. PCRE2 rejects the range. + * Leave ICU set operations alone. Skip comments, quoted text and control + * escapes because they can hide set boundaries. + * + * Check the rest of the pattern too. Rewriting the hyphen could make + * PCRE2 accept syntax ICU rejects, such as a Python-style named group, + * a quantified lookahead, a bare "\N" or the property "\p{Xan}". + */ + $escape_hyphens = PHP_VERSION_ID >= 70300 + && false !== strpos( $pattern, '-' ) + && false !== strpos( $pattern, '\\' ) + && preg_match( + '~\A(?: + [^(\[\]{}\\\\#]++ | \((?:\?:|(?![?*])) | + \{[0-9]++(?:,[0-9]*+)?\} | \\\\[dDhHsSvVwWAbBGZzafnrtKRX[:punct:]] | + \[\^?(?: + [a-zA-Z0-9._](?:-[a-zA-Z0-9._])?+ | + \\\\[dDhHsSvVwW]-?+ | \\\\[.\]-] | (?assertQuery( "SELECT $sql AS r" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpHyphenAfterShorthandCases() { + return array( + 'full email pattern' => array( "REGEXP_LIKE('first.last-x@my-site.example.com', '^[\\\\w-\\\\.]+@([\\\\w-]+\\\\.)+[\\\\w-]{2,4}\$')", '1' ), + 'email pattern, operator' => array( "'a@b.co' REGEXP '^[\\\\w-\\\\.]+@'", '1' ), + 'email pattern' => array( "REGEXP_LIKE('foo-bar@example.com', '^[\\\\w-.]+@[\\\\w-]+\\\\.\\\\w+\$')", '1' ), + 'email, trailing hyphen' => array( "REGEXP_LIKE('a.b@my-site.org', '^[\\\\w-.]+@[a-z0-9-]+\\\\.[a-z]+\$')", '1' ), + 'negated trailing hyphen' => array( "REGEXP_LIKE('a-', '^[\\\\w-.][^a-z-]\$')", '0' ), + 'literal backslash and N' => array( "REGEXP_LIKE('a\\\\N', '^[\\\\w-.]\\\\\\\\N\$')", '1' ), + 'noncapturing group' => array( "REGEXP_LIKE('abc', '^(?:[\\\\w-.]+)\$')", '1' ), + 'escaped parentheses' => array( "REGEXP_LIKE('(a)', '^\\\\([\\\\w-.]\\\\)\$')", '1' ), + 'anchors and tab escape' => array( "REGEXP_LIKE('a\t', '\\\\A[\\\\w-.]\\\\t\\\\z')", '1' ), + 'exact count' => array( "REGEXP_LIKE('a-', '^[\\\\w-.]{2}\$')", '1' ), + 'open count' => array( "REGEXP_LIKE('a-.', '^[\\\\w-.]{2,}\$')", '1' ), + 'escaped braces' => array( "REGEXP_LIKE('a{user}', '^[\\\\w-.]\\\\{user\\\\}\$')", '1' ), + // PCRE matches ² with \w and excludes U+200C. ICU gives the opposite results. + 'superscript in slug' => array( "REGEXP_LIKE('²', '^[\\\\w-.]+\$')", '1' ), + 'join control in slug' => array( "REGEXP_LIKE('a\u{200C}b', '^[\\\\w-.]+\$')", '0' ), + 'slug pattern' => array( "REGEXP_LIKE('a1', '^[a-z\\\\d-_]+\$')", '1' ), + + // A hyphen after a shorthand class matches itself. + '\\d, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\d-x]\$')", '1' ), + '\\d, shorthand' => array( "REGEXP_LIKE('5', '^[\\\\d-x]\$')", '1' ), + '\\d, last character' => array( "REGEXP_LIKE('x', '^[\\\\d-x]\$')", '1' ), + '\\d, past last character' => array( "REGEXP_LIKE('y', '^[\\\\d-x]\$')", '0' ), + '\\D, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\D-1]\$')", '1' ), + '\\D, last character' => array( "REGEXP_LIKE('1', '^[\\\\D-1]\$')", '1' ), + '\\D, other digit' => array( "REGEXP_LIKE('2', '^[\\\\D-1]\$')", '0' ), + '\\s, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\s-x]\$')", '1' ), + '\\s, other letter' => array( "REGEXP_LIKE('a', '^[\\\\s-x]\$')", '0' ), + '\\S, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\S-x]\$')", '1' ), + '\\S, space' => array( "REGEXP_LIKE(' ', '^[\\\\S-x]\$')", '0' ), + '\\w, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\w-.]\$')", '1' ), + '\\w, dot' => array( "REGEXP_LIKE('.', '^[\\\\w-.]\$')", '1' ), + '\\W, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\W-x]\$')", '1' ), + '\\W, other letter' => array( "REGEXP_LIKE('y', '^[\\\\W-x]\$')", '0' ), + '\\h, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\h-x]\$')", '1' ), + '\\H, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\H-x]\$')", '1' ), + '\\v, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\v-x]\$')", '1' ), + '\\V, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\V-x]\$')", '1' ), + 'last in the set' => array( "REGEXP_LIKE('-', '^[\\\\w-]\$')", '1' ), + 'before a shorthand' => array( "REGEXP_LIKE('-', '^[\\\\w-\\\\d]\$')", '1' ), + 'before an escape, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\w-\\\\]]\$')", '1' ), + 'before an escape, bracket' => array( "REGEXP_LIKE(']', '^[\\\\w-\\\\]]\$')", '1' ), + 'before a range, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\d-a-c]\$')", '1' ), + 'before a range, in range' => array( "REGEXP_LIKE('b', '^[\\\\d-a-c]\$')", '1' ), + 'before a range, past it' => array( "REGEXP_LIKE('e', '^[\\\\d-a-c]\$')", '0' ), + + 'outside of a set' => array( "REGEXP_LIKE('1-2', '^\\\\d-\\\\d\$')", '1' ), + 'with a quantifier' => array( "REGEXP_LIKE('1--', '^\\\\d-{2}\$')", '1' ), + + // An escaped character can start a range. + 'escaped backslash, range' => array( "REGEXP_LIKE('a', '^[\\\\\\\\-a]\$')", '1' ), + 'escaped backslash, past' => array( "REGEXP_LIKE('Z', '^[\\\\\\\\-a]\$')", '0' ), + 'control escape, range' => array( "REGEXP_LIKE('a', '^[\\\\t-x]\$')", '1' ), + 'control backslash, range' => array( "REGEXP_LIKE('m', '^[\\\\c\\\\d-z]\$')", '1' ), + 'control backslash, hyphen' => array( "REGEXP_LIKE('-', '^[\\\\c\\\\d-z]\$')", '0' ), + 'literal letter after it' => array( "REGEXP_LIKE('\\\\d-x', '^\\\\\\\\d-x\$')", '1' ), + 'quoted shorthand' => array( "REGEXP_LIKE('\\\\w-', '^\\\\Q\\\\w-\\\\E\$')", '1' ), + ); + } + + /** + * @dataProvider regexpSetSyntaxAfterShorthandCases + */ + public function testRegexpSetSyntaxAfterShorthand( $subject, $pattern ) { + // These patterns fall outside the hyphen rewrite. + $sql = sprintf( 'SELECT REGEXP_LIKE(%s, %s) AS r', $this->engine->quote( $subject ), $this->engine->quote( $pattern ) ); + if ( PHP_VERSION_ID < 70300 ) { + // PCRE 8 matches these subjects even when MySQL rejects the pattern or finds no match. + $this->assertQuery( $sql ); + $this->assertSame( '1', $this->last_result[0]->r ); + return; + } + $this->assertQueryError( $sql, "Invalid regular expression: $pattern." ); + } + + public static function regexpSetSyntaxAfterShorthandCases() { + return array( + 'trailing double hyphen' => array( '-', '^[\w--]$' ), + 'set difference' => array( '.', '^[\w--x]$' ), + 'nested set' => array( '5]', '^[\d-[5]]$' ), + 'hyphen before intersection' => array( 'a', '^[\w-x&&\d]$' ), + 'hyphen after intersection' => array( 'a', '^[\d&&\w-x]$' ), + 'hyphen in nested set' => array( '-]', '^[a&&[\w-x]]$' ), + 'POSIX class intersection' => array( 'a', '^[\d-x[:alpha:]&[:digit:]]$' ), + 'malformed count' => array( 'a{user}', '^[\w-.]{user}$' ), + 'unclosed count' => array( 'a{2', '^[\w-.]{2$' ), + 'counted lookahead' => array( 'ax', '^[\w-.](?=x){2}x$' ), + 'Python-style named group' => array( 'abc', '^(?P[\w-.]+)$' ), + 'PCRE ungreedy option' => array( 'abc', '^(?U)[\w-.]+$' ), + 'count without minimum' => array( 'a{,2}', '^[\w-.]{,2}$' ), + // MySQL matches the literal # here. A raw # disables the rewrite. + 'raw hash' => array( '#a', '^#[\w-.]$' ), + 'hash inside extended comment' => array( 'a-', '(?x)^[\w-.]{2}(?#a#)$' ), + 'bare named character escape' => array( 'ab', '^[\w-.]\N$' ), + 'property after set' => array( '-x', '^[\w-.]\p{Xan}$' ), + 'negated property before set' => array( '!-', '^\P{Xan}[\w-.]$' ), + 'Unicode property intersection' => array( '5', '^[\w-.\p{L}&[:alpha:]]$' ), + 'extended mode, spaced hyphens' => array( '-', '(?x)^[\w- -a]$' ), + 'stray end quote' => array( 'x-', '^\Ex[\w-.]$' ), + ); + } + /** * @dataProvider regexpBigIntegerCases */ diff --git a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Instr_Tests.php b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Instr_Tests.php index 64f1c94c6..72f63fb91 100644 --- a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Instr_Tests.php +++ b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Instr_Tests.php @@ -697,62 +697,10 @@ protected function get_known_failures(): array { 'icu-regextst:3839', ); - // PCRE2 rejects a range with a class at one end, like "[\d-z]". PCRE 8 reads the "-" literally. + // PCRE2 rejects the range in "[[:digit:]-z]". PCRE 8 treats the hyphen literally. if ( $is_pcre2 ) { array_push( $failures, - 'mysql-server-suite:9539', - 'mysql-server-suite:9540', - 'mysql-server-suite:9541', - 'mysql-server-suite:9542', - 'mysql-server-suite:9543', - 'mysql-server-suite:9544', - 'mysql-server-suite:9545', - 'mysql-server-suite:9546', - 'mysql-server-suite:9547', - 'mysql-server-suite:9548', - 'mysql-server-suite:9549', - 'mysql-server-suite:9550', - 'mysql-server-suite:9551', - 'mysql-server-suite:9552', - 'mysql-server-suite:9553', - 'mysql-server-suite:9554', - 'mysql-server-suite:9555', - 'mysql-server-suite:9556', - 'mysql-server-suite:9557', - 'mysql-server-suite:9558', - 'mysql-server-suite:9559', - 'mysql-server-suite:9560', - 'mysql-server-suite:9561', - 'mysql-server-suite:9562', - 'mysql-server-suite:9563', - 'mysql-server-suite:9564', - 'mysql-server-suite:9565', - 'mysql-server-suite:9566', - 'mysql-server-suite:9567', - 'mysql-server-suite:9568', - 'mysql-server-suite:9569', - 'mysql-server-suite:9570', - 'mysql-server-suite:9571', - 'mysql-server-suite:9572', - 'mysql-server-suite:9573', - 'mysql-server-suite:9574', - 'mysql-server-suite:9575', - 'mysql-server-suite:9576', - 'mysql-server-suite:9577', - 'mysql-server-suite:9578', - 'mysql-server-suite:9579', - 'mysql-server-suite:9580', - 'mysql-server-suite:9581', - 'mysql-server-suite:9582', - 'mysql-server-suite:9583', - 'mysql-server-suite:9584', - 'mysql-server-suite:9585', - 'mysql-server-suite:9586', - 'mysql-server-suite:9587', - 'mysql-server-suite:9588', - 'mysql-server-suite:9589', - 'mysql-server-suite:9590', 'mysql-server-suite:9617', 'mysql-server-suite:9618', 'mysql-server-suite:9619', @@ -782,33 +730,7 @@ protected function get_known_failures(): array { 'mysql-server-suite:9645', 'mysql-server-suite:9646', 'mysql-server-suite:9663', - 'mysql-server-suite:9664', - 'icu-regextst:1037', - 'icu-regextst:1038', - 'icu-regextst:2460', - 'icu-regextst:2461', - 'icu-regextst:2462', - 'icu-regextst:2463', - 'icu-regextst:2464', - 'icu-regextst:2465', - 'icu-regextst:2466', - 'icu-regextst:2467', - 'icu-regextst:2468', - 'icu-regextst:2469', - 'icu-regextst:2470', - 'icu-regextst:2471', - 'icu-regextst:3892', - 'icu-regextst:3893', - 'icu-regextst:3894', - 'icu-regextst:3895', - 'icu-regextst:3896', - 'icu-regextst:3897', - 'icu-regextst:3898', - 'icu-regextst:3899', - 'icu-regextst:3900', - 'icu-regextst:3901', - 'icu-regextst:3902', - 'icu-regextst:3903' + 'mysql-server-suite:9664' ); } diff --git a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php index 0e7b7a362..696492b32 100644 --- a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php +++ b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Like_Tests.php @@ -668,62 +668,10 @@ protected function get_known_failures(): array { 'icu-regextst:3839', ); - // PCRE2 rejects a range with a class at one end, like "[\d-z]". PCRE 8 reads the "-" literally. + // PCRE2 rejects the range in "[[:digit:]-z]". PCRE 8 treats the hyphen literally. if ( $is_pcre2 ) { array_push( $failures, - 'mysql-server-suite:9539', - 'mysql-server-suite:9540', - 'mysql-server-suite:9541', - 'mysql-server-suite:9542', - 'mysql-server-suite:9543', - 'mysql-server-suite:9544', - 'mysql-server-suite:9545', - 'mysql-server-suite:9546', - 'mysql-server-suite:9547', - 'mysql-server-suite:9548', - 'mysql-server-suite:9549', - 'mysql-server-suite:9550', - 'mysql-server-suite:9551', - 'mysql-server-suite:9552', - 'mysql-server-suite:9553', - 'mysql-server-suite:9554', - 'mysql-server-suite:9555', - 'mysql-server-suite:9556', - 'mysql-server-suite:9557', - 'mysql-server-suite:9558', - 'mysql-server-suite:9559', - 'mysql-server-suite:9560', - 'mysql-server-suite:9561', - 'mysql-server-suite:9562', - 'mysql-server-suite:9563', - 'mysql-server-suite:9564', - 'mysql-server-suite:9565', - 'mysql-server-suite:9566', - 'mysql-server-suite:9567', - 'mysql-server-suite:9568', - 'mysql-server-suite:9569', - 'mysql-server-suite:9570', - 'mysql-server-suite:9571', - 'mysql-server-suite:9572', - 'mysql-server-suite:9573', - 'mysql-server-suite:9574', - 'mysql-server-suite:9575', - 'mysql-server-suite:9576', - 'mysql-server-suite:9577', - 'mysql-server-suite:9578', - 'mysql-server-suite:9579', - 'mysql-server-suite:9580', - 'mysql-server-suite:9581', - 'mysql-server-suite:9582', - 'mysql-server-suite:9583', - 'mysql-server-suite:9584', - 'mysql-server-suite:9585', - 'mysql-server-suite:9586', - 'mysql-server-suite:9587', - 'mysql-server-suite:9588', - 'mysql-server-suite:9589', - 'mysql-server-suite:9590', 'mysql-server-suite:9617', 'mysql-server-suite:9618', 'mysql-server-suite:9619', @@ -757,33 +705,7 @@ protected function get_known_failures(): array { 'mysql-server-suite:9647', 'mysql-server-suite:9648', 'mysql-server-suite:9663', - 'mysql-server-suite:9664', - 'icu-regextst:1037', - 'icu-regextst:1038', - 'icu-regextst:2460', - 'icu-regextst:2461', - 'icu-regextst:2462', - 'icu-regextst:2463', - 'icu-regextst:2464', - 'icu-regextst:2465', - 'icu-regextst:2466', - 'icu-regextst:2467', - 'icu-regextst:2468', - 'icu-regextst:2469', - 'icu-regextst:2470', - 'icu-regextst:2471', - 'icu-regextst:3892', - 'icu-regextst:3893', - 'icu-regextst:3894', - 'icu-regextst:3895', - 'icu-regextst:3896', - 'icu-regextst:3897', - 'icu-regextst:3898', - 'icu-regextst:3899', - 'icu-regextst:3900', - 'icu-regextst:3901', - 'icu-regextst:3902', - 'icu-regextst:3903' + 'mysql-server-suite:9664' ); } diff --git a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Replace_Tests.php b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Replace_Tests.php index 4eae03f59..86ecb7061 100644 --- a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Replace_Tests.php +++ b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Replace_Tests.php @@ -722,62 +722,10 @@ protected function get_known_failures(): array { 'icu-regextst:3839', ); - // PCRE2 rejects a range with a class at one end, like "[\d-z]". PCRE 8 reads the "-" literally. + // PCRE2 rejects the range in "[[:digit:]-z]". PCRE 8 treats the hyphen literally. if ( $is_pcre2 ) { array_push( $failures, - 'mysql-server-suite:9539', - 'mysql-server-suite:9540', - 'mysql-server-suite:9541', - 'mysql-server-suite:9542', - 'mysql-server-suite:9543', - 'mysql-server-suite:9544', - 'mysql-server-suite:9545', - 'mysql-server-suite:9546', - 'mysql-server-suite:9547', - 'mysql-server-suite:9548', - 'mysql-server-suite:9549', - 'mysql-server-suite:9550', - 'mysql-server-suite:9551', - 'mysql-server-suite:9552', - 'mysql-server-suite:9553', - 'mysql-server-suite:9554', - 'mysql-server-suite:9555', - 'mysql-server-suite:9556', - 'mysql-server-suite:9557', - 'mysql-server-suite:9558', - 'mysql-server-suite:9559', - 'mysql-server-suite:9560', - 'mysql-server-suite:9561', - 'mysql-server-suite:9562', - 'mysql-server-suite:9563', - 'mysql-server-suite:9564', - 'mysql-server-suite:9565', - 'mysql-server-suite:9566', - 'mysql-server-suite:9567', - 'mysql-server-suite:9568', - 'mysql-server-suite:9569', - 'mysql-server-suite:9570', - 'mysql-server-suite:9571', - 'mysql-server-suite:9572', - 'mysql-server-suite:9573', - 'mysql-server-suite:9574', - 'mysql-server-suite:9575', - 'mysql-server-suite:9576', - 'mysql-server-suite:9577', - 'mysql-server-suite:9578', - 'mysql-server-suite:9579', - 'mysql-server-suite:9580', - 'mysql-server-suite:9581', - 'mysql-server-suite:9582', - 'mysql-server-suite:9583', - 'mysql-server-suite:9584', - 'mysql-server-suite:9585', - 'mysql-server-suite:9586', - 'mysql-server-suite:9587', - 'mysql-server-suite:9588', - 'mysql-server-suite:9589', - 'mysql-server-suite:9590', 'mysql-server-suite:9617', 'mysql-server-suite:9618', 'mysql-server-suite:9619', @@ -807,33 +755,7 @@ protected function get_known_failures(): array { 'mysql-server-suite:9645', 'mysql-server-suite:9646', 'mysql-server-suite:9663', - 'mysql-server-suite:9664', - 'icu-regextst:1037', - 'icu-regextst:1038', - 'icu-regextst:2460', - 'icu-regextst:2461', - 'icu-regextst:2462', - 'icu-regextst:2463', - 'icu-regextst:2464', - 'icu-regextst:2465', - 'icu-regextst:2466', - 'icu-regextst:2467', - 'icu-regextst:2468', - 'icu-regextst:2469', - 'icu-regextst:2470', - 'icu-regextst:2471', - 'icu-regextst:3892', - 'icu-regextst:3893', - 'icu-regextst:3894', - 'icu-regextst:3895', - 'icu-regextst:3896', - 'icu-regextst:3897', - 'icu-regextst:3898', - 'icu-regextst:3899', - 'icu-regextst:3900', - 'icu-regextst:3901', - 'icu-regextst:3902', - 'icu-regextst:3903' + 'mysql-server-suite:9664' ); } diff --git a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Substr_Tests.php b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Substr_Tests.php index ae48e3237..82e675daf 100644 --- a/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Substr_Tests.php +++ b/packages/mysql-on-sqlite/tests/regexp/WP_MySQL_On_SQLite_Regexp_Substr_Tests.php @@ -697,62 +697,10 @@ protected function get_known_failures(): array { 'icu-regextst:3839', ); - // PCRE2 rejects a range with a class at one end, like "[\d-z]". PCRE 8 reads the "-" literally. + // PCRE2 rejects the range in "[[:digit:]-z]". PCRE 8 treats the hyphen literally. if ( $is_pcre2 ) { array_push( $failures, - 'mysql-server-suite:9539', - 'mysql-server-suite:9540', - 'mysql-server-suite:9541', - 'mysql-server-suite:9542', - 'mysql-server-suite:9543', - 'mysql-server-suite:9544', - 'mysql-server-suite:9545', - 'mysql-server-suite:9546', - 'mysql-server-suite:9547', - 'mysql-server-suite:9548', - 'mysql-server-suite:9549', - 'mysql-server-suite:9550', - 'mysql-server-suite:9551', - 'mysql-server-suite:9552', - 'mysql-server-suite:9553', - 'mysql-server-suite:9554', - 'mysql-server-suite:9555', - 'mysql-server-suite:9556', - 'mysql-server-suite:9557', - 'mysql-server-suite:9558', - 'mysql-server-suite:9559', - 'mysql-server-suite:9560', - 'mysql-server-suite:9561', - 'mysql-server-suite:9562', - 'mysql-server-suite:9563', - 'mysql-server-suite:9564', - 'mysql-server-suite:9565', - 'mysql-server-suite:9566', - 'mysql-server-suite:9567', - 'mysql-server-suite:9568', - 'mysql-server-suite:9569', - 'mysql-server-suite:9570', - 'mysql-server-suite:9571', - 'mysql-server-suite:9572', - 'mysql-server-suite:9573', - 'mysql-server-suite:9574', - 'mysql-server-suite:9575', - 'mysql-server-suite:9576', - 'mysql-server-suite:9577', - 'mysql-server-suite:9578', - 'mysql-server-suite:9579', - 'mysql-server-suite:9580', - 'mysql-server-suite:9581', - 'mysql-server-suite:9582', - 'mysql-server-suite:9583', - 'mysql-server-suite:9584', - 'mysql-server-suite:9585', - 'mysql-server-suite:9586', - 'mysql-server-suite:9587', - 'mysql-server-suite:9588', - 'mysql-server-suite:9589', - 'mysql-server-suite:9590', 'mysql-server-suite:9617', 'mysql-server-suite:9618', 'mysql-server-suite:9619', @@ -782,33 +730,7 @@ protected function get_known_failures(): array { 'mysql-server-suite:9645', 'mysql-server-suite:9646', 'mysql-server-suite:9663', - 'mysql-server-suite:9664', - 'icu-regextst:1037', - 'icu-regextst:1038', - 'icu-regextst:2460', - 'icu-regextst:2461', - 'icu-regextst:2462', - 'icu-regextst:2463', - 'icu-regextst:2464', - 'icu-regextst:2465', - 'icu-regextst:2466', - 'icu-regextst:2467', - 'icu-regextst:2468', - 'icu-regextst:2469', - 'icu-regextst:2470', - 'icu-regextst:2471', - 'icu-regextst:3892', - 'icu-regextst:3893', - 'icu-regextst:3894', - 'icu-regextst:3895', - 'icu-regextst:3896', - 'icu-regextst:3897', - 'icu-regextst:3898', - 'icu-regextst:3899', - 'icu-regextst:3900', - 'icu-regextst:3901', - 'icu-regextst:3902', - 'icu-regextst:3903' + 'mysql-server-suite:9664' ); } From 5521c371b9b1ef9ba883ab5e04bfb87b3c65b00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Jake=C5=A1?= Date: Thu, 3 Dec 2026 14:04:44 +0100 Subject: [PATCH 2/2] Preserve whole characters in negated REGEXP sets on old PCRE Before PCRE2 10.40, JIT can split a UTF-8 character when a caseless negated set excludes one letter. Disable JIT for UTF patterns that may contain such a set, including patterns that enable UTF or folding inline. Keep JIT for common exclusions such as punctuation, shorthands, POSIX classes and ranges with distinct endpoints. Quoted text that looks like a negated set can also select the interpreter. Long scans can slow down and queries can reach a backtrack or depth limit. The version check includes builds with a backported fix. PHP 7.2 uses PCRE 8, which has no per-pattern JIT switch. Repeat a literal letter in a negated set there. Escaped letters, equivalent ranges, and patterns with control escapes or quotes inside comments can still be affected. --- .github/workflows/phpunit-tests.yml | 22 +++ ...s-wp-sqlite-pdo-user-defined-functions.php | 70 +++++++++- .../tests/WP_MySQL_On_SQLite_Tests.php | 129 ++++++++++++++++++ 3 files changed, 214 insertions(+), 7 deletions(-) diff --git a/.github/workflows/phpunit-tests.yml b/.github/workflows/phpunit-tests.yml index 67b744fa0..4161ba58f 100644 --- a/.github/workflows/phpunit-tests.yml +++ b/.github/workflows/phpunit-tests.yml @@ -129,3 +129,25 @@ jobs: - name: Run PHPUnit suite run: php ./vendor/bin/phpunit -c ./phpunit.xml.dist working-directory: packages/mysql-on-sqlite + + - name: Check REGEXP matching with PCRE2 10.39 and JIT + if: matrix.php == '8.1' + run: | + docker run --rm -i --network none \ + -v "$GITHUB_WORKSPACE:/project:ro" -w /project/packages/mysql-on-sqlite \ + php:8.1.34-cli@sha256:76e563191d1ade120313a8736df24154d21da5155c0756f147c0b01bd19d9087 \ + bash -eo pipefail <<'SH' + # This match succeeds only with JIT at the selected depth limit. + php -d pcre.jit=1 -d pcre.recursion_limit=100 -r ' + exit(explode(" ", PCRE_VERSION)[0] === "10.39" && + preg_match("/^(item,)+$/", str_repeat("item,", 100)) === 1 ? 0 : 1); + ' + # Use ASCII depth tests to distinguish JIT from the interpreter on this old engine. + # The punctuation case checks a whole UTF-8 character on the JIT path. + php -d pcre.jit=1 vendor/bin/phpunit --do-not-cache-result \ + --filter 'testRegexpNegated.*DepthLimit|testRegexpNegatedSetOfOneLetter.*punctuation' \ + --log-junit /tmp/regexp.xml + for method in SetUsesNativeDepthLimit LetterUsesInterpreterDepthLimit SetOfOneLetter; do + grep -q "name=\"testRegexpNegated$method" /tmp/regexp.xml + done + SH diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 8d290d616..f8d1eb565 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -1322,13 +1322,17 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w * characters like "ą" (0xC4 0x85). (*ANYCRLF) misses only the rare VT and FF. */ if ( $unix_lines ) { - $newline = '(*LF)'; + $prefix = '(*LF)'; } elseif ( $binary_mode ) { - $newline = '(*ANYCRLF)'; + $prefix = '(*ANYCRLF)'; } else { - $newline = '(*ANY)'; + $prefix = '(*ANY)'; } - $compiled = '/' . $newline . $this->regexp_escape_pattern( $pattern, $without_g ) . '/' . $modifiers; + + if ( $this->regexp_requires_no_jit( $pattern, $case_sensitive, $binary_mode ) ) { + $prefix = '(*NO_JIT)' . $prefix; + } + $compiled = '/' . $prefix . $this->regexp_escape_pattern( $pattern, $without_g ) . '/' . $modifiers; /* * Compile the pattern by matching it against an empty string. Only a compile @@ -1353,9 +1357,44 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w } /** - * Escape pattern delimiters and NUL bytes that are not already escaped, read - * "\K" as "K", escape a hyphen after a shorthand class, and, if asked, make - * "\G" never match. + * Before PCRE2 10.40, JIT can split a UTF-8 character or crash PHP when + * a case-insensitive set excludes a single letter. Inline options can + * enable case folding or turn on UTF in a binary pattern. + * + * Before the caret, PCRE2 skips "\E" and empty "\Q\E" quotes. In "(?xx)" + * mode it also skips spaces and tabs. Keep JIT when the set clearly + * excludes a character with no case variants or multiple distinct characters. + * + * Quoted text can look like a set and select the interpreter too. + * Without JIT, long scans can slow down or hit a backtrack or depth + * limit. The version check also covers builds with a backported fix. + * + * @param string $pattern The MySQL pattern. + * @param bool $case_sensitive Whether matching starts in case-sensitive mode. + * @param bool $binary_mode Whether matching starts in byte mode. + * @return bool Whether to disable JIT for this pattern. + */ + private function regexp_requires_no_jit( $pattern, $case_sensitive, $binary_mode ) { + return PHP_VERSION_ID >= 70300 && false !== strpos( $pattern, '^' ) + && ( ! $binary_mode || false !== strpos( $pattern, '(*UTF' ) ) + && version_compare( explode( ' ', PCRE_VERSION )[0], '10.40', '<' ) + && ( ! $case_sensitive || 0 !== preg_match( '/\(\?\^?[a-zA-Z]*i/', $pattern ) ) + && 0 !== preg_match( + '~\[(?:[ \t]|\\\\E|\\\\Q\\\\E)*+\^ (?! + [ ]\] | (?![a-zA-Z\[\\\\])[\x21-\x7e] # Space alone, ASCII digit or punctuation. + | \\\\ (?:[dDhHsSvVwW]|(?=[\x21-\x7e])[[:punct:]]) # Shorthand class or escaped punctuation. + | \[:\^?[a-z]++:\] # POSIX class. + | ([a-zA-Z]) (?: + -(?!\1)[a-zA-Z0-9] # Distinct range endpoints. + | (?!(?i:\1)|[-\[\]\\\\])[\x21-\x7e] # A second literal distinct under /i. + ) + )~x', + $pattern + ); + } + + /** + * Prepare a MySQL regex for use as a slash-delimited PCRE pattern. * * PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00". * @@ -1405,6 +1444,16 @@ private function regexp_escape_pattern( $pattern, $without_g = false ) { $pattern ); + /* + * PCRE 8 has no "(*NO_JIT)". Its JIT can split a UTF-8 character when + * matching "[^s]" case-insensitively. Writing "[^ss]" avoids the bug. + * Equivalent forms such as "[^\x73]" and "[^s-s]" can still split a character. + * + * Skip patterns with "\c". In "\c\\Q", the control escape consumes + * the first backslash, so the second backslash starts a "\Q" quote. + */ + $splits_characters = PHP_VERSION_ID < 70300 && false === strpos( $pattern, '\c' ); + $escaped = ''; $quoted = false; $length = strlen( $pattern ); @@ -1432,6 +1481,13 @@ private function regexp_escape_pattern( $pattern, $without_g = false ) { $escaped .= '\\-'; ++$i; } + } elseif ( + $splits_characters && '[' === $character + // (*NO_START_OPT) avoids searching for "]" at every "[". + && preg_match( '/(*NO_START_OPT)\[\^([a-zA-Z]|[\xC2-\xF4][\x80-\xBF]{1,3})\]/A', $pattern, $match, 0, $i ) + ) { + $escaped .= '[^' . $match[1] . $match[1] . ']'; + $i += strlen( $match[0] ) - 1; } elseif ( isset( $special[ $character ] ) ) { $escaped .= $special[ $character ]; } else { diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 4ceffc760..13398220a 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -526,6 +526,62 @@ public function testRegexpJitStackLimit() { ); } + /** + * @dataProvider regexpNegatedSetDepthLimitCases + */ + public function testRegexpNegatedSetUsesNativeDepthLimit( $separator, $pattern, $match_type ) { + $this->iniSet( 'pcre.recursion_limit', '100' ); + $subject = implode( $separator, array_fill( 0, 100, 'item' ) ); + $sql = sprintf( "SELECT REGEXP_LIKE('%s', %s, '%s') AS r", $subject, $this->engine->quote( $pattern ), $match_type ); + $modifiers = 'c' === $match_type ? 'uX' : 'iuX'; + + // Native matching can hit the depth limit when PHP runs without JIT. + if ( false === preg_match( "/(*ANY)$pattern/$modifiers", $subject ) ) { + $this->assertSame( PREG_RECURSION_LIMIT_ERROR, preg_last_error() ); + $this->assertQueryError( $sql, 'Regular expression evaluation exceeded internal limits.' ); + return; + } + + $this->assertQuery( $sql ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + + public static function regexpNegatedSetDepthLimitCases() { + return array( + 'case-sensitive letter' => array( 'x', '^[^x]+(x[^x]+)*$', 'c' ), + 'comma, default case' => array( ',', '^[^,]+(,[^,]+)*$', '' ), + 'space, default case' => array( ' ', '^[^ ]+( [^ ]+)*$', '' ), + 'digit, default case' => array( '0', '^[^0]+(0[^0]+)*$', '' ), + 'escaped punctuation' => array( '.', '^[^\.]+(\.[^\.]+)*$', '' ), + 'escaped bracket' => array( ']', '^[^\]x]+(\][^\]x]+)*$', '' ), + 'letter range' => array( 'a', '^[^a-d]+(a[^a-d]+)*$', '' ), + 'letter pair' => array( 'x', '^[^xy]+(x[^xy]+)*$', '' ), + 'digit shorthand' => array( '1', '^[^\d]+(1[^\d]+)*$', '' ), + 'POSIX class' => array( '1', '^[^[:digit:]]+(1[^[:digit:]]+)*$', '' ), + 'letter then comma' => array( ',', '^[^x,]+(,[^x,]+)*$', '' ), + 'comma then letter' => array( ',', '^[^,x]+(,[^,x]+)*$', '' ), + 'punctuation pair' => array( ',', '^[^,.]+(,[^,.]+)*$', '' ), + 'punctuation and class' => array( '@', '^[^@\s]+(@[^@\s]+)*$', '' ), + ); + } + + public function testRegexpNegatedLetterUsesInterpreterDepthLimitOnOldPcre2() { + $this->iniSet( 'pcre.recursion_limit', '100' ); + $subject = str_repeat( 'itemx', 100 ); + $pattern = '^([^x]+x)+$'; + $sql = sprintf( "SELECT REGEXP_LIKE('%s', %s) AS r", $subject, $this->engine->quote( $pattern ) ); + + $prefix = PHP_VERSION_ID >= 70300 && version_compare( explode( ' ', PCRE_VERSION )[0], '10.40', '<' ) ? '(*NO_JIT)' : ''; + if ( false === preg_match( "/$prefix(*ANY)$pattern/iuX", $subject ) ) { + $this->assertSame( PREG_RECURSION_LIMIT_ERROR, preg_last_error() ); + $this->assertQueryError( $sql, 'Regular expression evaluation exceeded internal limits.' ); + return; + } + + $this->assertQuery( $sql ); + $this->assertSame( '1', $this->last_result[0]->r ); + } + public function testRegexpLimitWithoutJit() { // Matching without JIT has a limit, too. if ( PHP_VERSION_ID < 70300 ) { @@ -1581,6 +1637,79 @@ public static function regexpSetSyntaxAfterShorthandCases() { ); } + /** + * @dataProvider regexpNegatedSetOfOneLetterCases + */ + public function testRegexpNegatedSetOfOneLetter( $sql, $expected ) { + $this->assertQuery( "SELECT $sql AS r" ); + $this->assertSame( $expected, $this->last_result[0]->r ); + } + + public static function regexpNegatedSetOfOneLetterCases() { + return array( + // Check the bytes so a partial UTF-8 character is visible on failure. + 'one match' => array( "HEX(REGEXP_REPLACE('日本s', '[^s]', '-', 1, 1))", '2DE69CAC73' ), + 'all matches' => array( "REGEXP_REPLACE('日本s', '[^s]', '-')", '--s' ), + 'REGEXP_SUBSTR' => array( "REGEXP_SUBSTR('日本', '[^s]')", '日' ), + 'REGEXP_INSTR' => array( "REGEXP_INSTR('s日', '[^s]', 1, 1, 1)", '3' ), + 'anchored' => array( "REGEXP_LIKE('日', '^[^s]\$')", '1' ), + 'operator' => array( "'日' REGEXP '^[^s]\$'", '1' ), + // MySQL 8.4 rejects a binary pattern with a text subject. + 'binary, inline UTF' => array( "'日' REGEXP BINARY '(*UTF)(?i)^[^s]\$'", '1' ), + 'binary, byte mode' => array( "'日' REGEXP BINARY '(?i)^[^s]\$'", '0' ), + 'punctuation' => array( "REGEXP_REPLACE('日本,', '[^,]', '-')", '--,' ), + 'between characters' => array( "REGEXP_LIKE('a日b', 'a[^s]b')", '1' ), + 'with a count' => array( "REGEXP_REPLACE('日本', '[^s]{2}', '-')", '-' ), + 'uppercase' => array( "REGEXP_REPLACE('日S', '[^S]', '-')", '-S' ), + 'Greek letter' => array( "REGEXP_REPLACE('日σ', '[^σ]', '-')", '-σ' ), + 'three-byte letter' => array( "REGEXP_REPLACE('日ṡ', '[^ṡ]', '-')", '-ṡ' ), + 'other cases' => array( "REGEXP_REPLACE('sSſx', '[^s]', '-')", 'sSſ-' ), + 'other cases, Greek' => array( "REGEXP_REPLACE('σςΣx', '[^σ]', '-')", 'σςΣ-' ), + 'case-sensitive' => array( "REGEXP_REPLACE('sSx', '[^s]', '-', 1, 0, 'c')", 's--' ), + 'quoted' => array( "REGEXP_LIKE('[^s]', '^\\\\Q[^s]\\\\E\$')", '1' ), + 'control then quote' => array( "REGEXP_LIKE('\x1C[^s]', '^\\\\c\\\\\\\\Q[^s]\\\\E\$')", '1' ), + 'extra quoted s' => array( "REGEXP_LIKE('\x1C[^ss]', '^\\\\c\\\\\\\\Q[^s]\\\\E\$')", '0' ), + ); + } + + /** + * @requires PHP >= 7.3 + * @dataProvider regexpNegatedSetOnPcre2Cases + */ + public function testRegexpNegatedSetOnPcre2( $pattern, $match_type, $min_pcre_version = null ) { + $quoted_pattern = $this->engine->quote( $pattern ); + $match_type = $this->engine->quote( $match_type ); + $sql = "SELECT HEX(REGEXP_SUBSTR('日', $quoted_pattern, 1, 1, $match_type)) AS r, + REGEXP_REPLACE('日', $quoted_pattern, '-', 1, 1, $match_type) AS replaced"; + + if ( null !== $min_pcre_version && version_compare( explode( ' ', PCRE_VERSION )[0], $min_pcre_version, '<' ) ) { + $this->assertQueryError( $sql, "Invalid regular expression: $pattern." ); + return; + } + + $this->assertQuery( $sql ); + $this->assertSame( 'E697A5', $this->last_result[0]->r ); + $this->assertSame( '-', $this->last_result[0]->replaced ); + } + + public static function regexpNegatedSetOnPcre2Cases() { + return array( + 'empty quote before caret' => array( '[\Q\E^s]', '' ), + 'space before caret' => array( '(?xx)[ ^s]', '' ), + // PCRE ignores a stray "\E". MySQL 8.4 finds no match for these two patterns. + 'end quote before caret' => array( '[\E^s]', '' ), + 'mixed before caret' => array( "(?xx)[\\E \t\\Q\\E^s]", '' ), + 'quote inside a comment' => array( "(?x)#\\Q\n[^s]", '' ), + 'hex escape' => array( '[^\x73]', '' ), + 'range at a word boundary' => array( '[^s-s]\b', '' ), + 'combined inline options' => array( '(?mi:[^s-s])', 'ic' ), + 'inline case folding' => array( '(?i:[^s-s])', 'c' ), + 'case-sensitive multibyte' => array( '[^s]', 'c' ), + // PCRE2 added this inline option reset in 10.32. MySQL 8.4 rejects it. + 'reset and enable folding' => array( '(?^i:[^s-s])', 'c', '10.32' ), + ); + } + /** * @dataProvider regexpBigIntegerCases */