Skip to content

Commit 586b97d

Browse files
committed
Keep REGEXP comments out of the translation
A translation in a "(?#...)" comment could end the comment early, as "\b" becomes a group with ")", or raise an error, as for an invalid set. So "a(?#\b)b" failed to compile, where MySQL matches "ab". Read each comment as one token, and replace it with an empty comment. Like in ICU, a "\Q" in a comment quotes its text up to "\E", so "(?#\Q)a" has no end, which is an error.
1 parent 03b2d7a commit 586b97d

2 files changed

Lines changed: 39 additions & 8 deletions

File tree

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,8 @@ private function regexp_check_match_type( $function_name, $match_type ) {
13361336
* - ICU attaches a combining mark to the character before it, so a mark after a
13371337
* space doesn't start a word for `\b`. Here, it does.
13381338
* - `\G` in a lookbehind doesn't always match where it does in ICU.
1339+
* - A `#` comment of the `x` flag is translated like the rest of the pattern, so
1340+
* an invalid set in it is an error, and a `\Q` in it quotes the text after it.
13391341
* - Data that isn't valid UTF-8 is matched byte by byte and case-sensitively,
13401342
* like MySQL binary strings. Other binary values match as text, so case
13411343
* sensitivity, and whether positions count bytes, can differ between rows.
@@ -1447,15 +1449,16 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false, $w
14471449
/**
14481450
* Translate a MySQL regex pattern to PCRE syntax.
14491451
*
1450-
* Splits the pattern into quoted text, sets, escapes, POSIX classes, and other
1451-
* characters, so that a translation applies only to a whole token.
1452+
* Splits the pattern into quoted text, comments, sets, escapes, POSIX classes, and
1453+
* other characters, so that a translation applies only to a whole token.
14521454
*
14531455
* @param string $pattern The regex pattern.
14541456
* @param bool $case_insensitive Whether the pattern ignores case by default.
14551457
* @param bool $binary_mode Whether the pattern matches bytes instead of UTF-8 characters.
14561458
* @param bool $without_g Whether "\G" never matches.
14571459
*
1458-
* @throws Exception If the pattern can't be split, or it has an invalid set.
1460+
* @throws Exception If the pattern can't be split, or it has an invalid set or a
1461+
* comment without an end.
14591462
* @return string Pattern safe to wrap in slash delimiters.
14601463
*/
14611464
private function regexp_translate_pattern( $pattern, $case_insensitive, $binary_mode, $without_g ) {
@@ -1506,6 +1509,10 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $binary_
15061509
}
15071510

15081511
/*
1512+
* A "(?#...)" comment is one token, so that no translation changes it. Like in
1513+
* ICU, a "\Q" in it quotes the text up to "\E", which can hold its ")", but "\\Q"
1514+
* is an escaped backslash and a "Q".
1515+
*
15091516
* A set, with its nested sets, is one token, as ICU's sets differ from PCRE's.
15101517
* Like ICU, "[:" starts a POSIX class if its name, which ends at the next ":",
15111518
* is followed by "]", and a set otherwise. ICU reads a POSIX class outside of a
@@ -1518,12 +1525,19 @@ private function regexp_translate_pattern( $pattern, $case_insensitive, $binary_
15181525
* scan the rest of the pattern each time.
15191526
*/
15201527
$translated = preg_replace_callback(
1521-
'/\\\\Q.*?(?:\\\\E|\z)|\[:\^?[^:]*:\]|(\[\^?+\]?+(?:\\\\Q.*?(?:\\\\E|\z)|\\\\c.|\\\\.|\[:\^?[^:]*:\]|(?1)|[^\\\\\[\]])*+\])|' . self::REGEXP_CODE_POINT_ESCAPE . '|\\\\[pP]\{[^}\\\\]*\}|\\\\.|[\/\0]/s',
1528+
'/\\\\Q.*?(?:\\\\E|\z)|\(\?#(?:\\\\Q.*?(?:\\\\E|\z)|\\\\[^)]|[^)])*+\)|\(\?#|\[:\^?[^:]*:\]|(\[\^?+\]?+(?:\\\\Q.*?(?:\\\\E|\z)|\\\\c.|\\\\.|\[:\^?[^:]*:\]|(?1)|[^\\\\\[\]])*+\])|' . self::REGEXP_CODE_POINT_ESCAPE . '|\\\\[pP]\{[^}\\\\]*\}|\\\\.|[\/\0]/s',
15221529
function ( $parts ) use ( $pattern, $tokens ) {
15231530
$token = $parts[0];
15241531
if ( isset( $parts[1] ) ) {
15251532
return $this->regexp_translate_set( $token, $pattern, $tokens );
15261533
}
1534+
if ( '(?#' === $token ) {
1535+
// The comment has no end, as its ")" is missing or quoted.
1536+
throw new Exception( "Invalid regular expression: $pattern." );
1537+
}
1538+
if ( '(' === $token[0] ) {
1539+
return '(?#)';
1540+
}
15271541
if ( '[' === $token[0] ) {
15281542
return '[' . $this->regexp_translate_property( $token, $tokens ) . ']';
15291543
}
@@ -1948,7 +1962,8 @@ private function regexp_matches( $pattern, $match_type, $byte_mode, $subject, $o
19481962
/*
19491963
* Only PCRE's "\K" can start a match before the search, and then the next
19501964
* search would find the same match forever. The translation reads "\K" as
1951-
* "K", but not after a "\Q" in a comment, which it reads as quoted text.
1965+
* "K", but not after a "\Q" in an extended comment, which it reads as quoted
1966+
* text.
19521967
*/
19531968
if ( $match[0][1] < $offset ) {
19541969
throw new Exception( 'Invalid regular expression: ' . $pattern . '.' );

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ public static function regexpLikeErrors() {
383383
'invalid pattern' => array( "REGEXP_LIKE('abc', '(abc')", 'Invalid regular expression: (abc.' ),
384384
'short escape' => array( "REGEXP_LIKE('u004', '\\\\u004')", 'Invalid regular expression: \\u004.' ),
385385
'bad property' => array( "REGEXP_LIKE('a', '\\\\p{Foo}')", 'Invalid regular expression: \\p{Foo}.' ),
386+
'open comment' => array( "REGEXP_LIKE('a', 'a(?#')", 'Invalid regular expression: a(?#.' ),
387+
'comment quote' => array( "REGEXP_LIKE('a', '(?#\\\\Q)a')", 'Invalid regular expression: (?#\\Q)a.' ),
386388
'property with /' => array( "REGEXP_LIKE('a', '\\\\p{a/i}')", 'Invalid regular expression: \\p{a/i}.' ),
387389
'extra argument' => array(
388390
"REGEXP_LIKE('abc', 'abc', 'c', 'extra')",
@@ -1504,9 +1506,10 @@ public function testRegexpEscapedKInLookbehind() {
15041506
* @dataProvider regexpQuoteInCommentCases
15051507
*/
15061508
public function testRegexpEscapedKAfterQuoteInComment( $sql, $pattern ) {
1507-
// The translation reads a "\Q" in a comment as quoted text, so a later "\K"
1508-
// reaches PCRE. A match that starts before the search is then an error, not an
1509-
// endless loop.
1509+
// The translation reads a "\Q" in an extended comment as quoted text, so a later
1510+
// "\K" reaches PCRE. A match that starts before the search is then an error, not an
1511+
// endless loop. In a "(?#...)" comment, "\Q" quotes the ")", like in ICU, so the
1512+
// comment has no end, which is an error too.
15101513
$this->assertQueryError(
15111514
'SELECT ' . sprintf( $sql, $this->engine->quote( $pattern ) ),
15121515
"Invalid regular expression: $pattern."
@@ -1603,6 +1606,7 @@ public function testRegexpBigIntegerColumn() {
16031606
* @dataProvider regexpLiteralEscapeCases
16041607
* @dataProvider regexpWordCases
16051608
* @dataProvider regexpPropertyCases
1609+
* @dataProvider regexpCommentCases
16061610
*/
16071611
public function testRegexpIcuSyntax( $pattern, $subject, $expected ) {
16081612
$this->assertQuery(
@@ -1793,6 +1797,18 @@ public static function regexpPropertyCases() {
17931797
);
17941798
}
17951799

1800+
public static function regexpCommentCases() {
1801+
return array(
1802+
'escape in a comment' => array( 'a(?#\b)b', 'ab', '<ab>' ),
1803+
'set in a comment' => array( '(?#[a--])a', 'a', '<a>' ),
1804+
'quoted ")" in a comment' => array( '(?#\Q)a\E)b', 'ab', 'a<b>' ),
1805+
'slash in a comment' => array( '(?#/)a', 'a', '<a>' ),
1806+
'comment before a digit' => array( '(a)\1(?#)0', 'aa0', '<aa0>' ),
1807+
'comment in a set' => array( '[(?#)]+', '#)a', '<#)>a' ),
1808+
'"\\\\Q" in a comment' => array( '(?#\\\\Q)a', 'a', '<a>' ),
1809+
);
1810+
}
1811+
17961812
/**
17971813
* @dataProvider regexpInvalidSetCases
17981814
*/

0 commit comments

Comments
 (0)