Skip to content

Commit b382fad

Browse files
committed
Pass REGEXP BINARY to the REGEXP function as a flag
The operator translation marked REGEXP BINARY by prepending a NUL byte to the pattern. Any pattern value that started with a NUL byte was then silently matched as REGEXP BINARY: the byte was dropped and matching became case-sensitive. Call the REGEXP function directly with an explicit flag instead, and escape NUL bytes in patterns, which PHP rejects before 8.2. Wrapping the call in parentheses also keeps the MySQL precedence, where REGEXP binds tighter than comparison operators. SQLite read 1 = 'a' REGEXP 'a' as (1 = 'a') REGEXP 'a'.
1 parent b2e5bff commit b382fad

3 files changed

Lines changed: 97 additions & 48 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,26 +5169,16 @@ private function translate_regexp_predicate( WP_Parser_Node $node, WP_Parser_Nod
51695169
$is_binary = isset( $tokens[1] ) && WP_MySQL_Lexer::BINARY_SYMBOL === $tokens[1]->id;
51705170
$subject = $this->translate( $node->get_first_child_node() );
51715171
$pattern = $this->translate( $operations->get_first_child_node() );
5172+
$not = null === $node->get_first_child_node( 'notRule' ) ? '' : 'NOT ';
51725173

51735174
/*
5174-
* If the query says REGEXP BINARY, the comparison is byte-by-byte
5175-
* and letter casing matters – lowercase and uppercase letters are
5176-
* represented using different byte codes.
5175+
* Call the REGEXP function directly, so that REGEXP BINARY can pass
5176+
* a flag for byte-by-byte, case-sensitive matching.
51775177
*
5178-
* The REGEXP function can't be easily made to accept two
5179-
* parameters, so we'll have to use a hack to get around this.
5180-
*
5181-
* If the first character of the pattern is a null byte, we'll
5182-
* remove it and make the comparison case-sensitive. This should
5183-
* be reasonably safe since PHP does not allow null bytes in
5184-
* regular expressions anyway.
5178+
* The parentheses keep the MySQL precedence. In SQLite, REGEXP binds
5179+
* as loosely as "=", so "1 = 'a' REGEXP 'a'" would be "(1 = 'a') REGEXP 'a'".
51855180
*/
5186-
if ( true === $is_binary ) {
5187-
$pattern = 'CHAR(0) || ' . $pattern;
5188-
}
5189-
5190-
$operator = null === $node->get_first_child_node( 'notRule' ) ? 'REGEXP' : 'NOT REGEXP';
5191-
return "$subject $operator $pattern";
5181+
return sprintf( '(%sREGEXP(%s, %s%s))', $not, $pattern, $subject, $is_binary ? ', 1' : '' );
51925182
}
51935183

51945184
/**

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

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static function register_for( $pdo ): self {
101101
* @var array
102102
*/
103103
private $function_arities = array(
104+
'regexp' => array( 2, 3 ),
104105
'regexp_like' => array( 2, 3 ),
105106
'regexp_replace' => array( 3, 4, 5, 6 ),
106107
'regexp_substr' => array( 2, 3, 4, 5 ),
@@ -592,44 +593,26 @@ public function isnull( $field ) {
592593
}
593594

594595
/**
595-
* Method to emulate MySQL REGEXP() function.
596+
* Method to emulate the MySQL REGEXP operator.
596597
*
597598
* @param string|null $pattern Regular expression to match.
598599
* @param string|null $field Haystack.
600+
* @param int $binary 1 for REGEXP BINARY, which matches bytes
601+
* case-sensitively; 0 otherwise.
599602
*
600603
* @throws Exception If the pattern is not a valid regular expression.
601604
* @return int|null 1 if matched, 0 if not matched, or NULL for NULL input.
602605
*/
603-
public function regexp( $pattern, $field ) {
606+
public function regexp( $pattern, $field, $binary = 0 ) {
604607
if ( null === $pattern || null === $field ) {
605608
return null;
606609
}
607-
$pattern = (string) $pattern;
608-
609-
/*
610-
* If the original query says REGEXP BINARY
611-
* the comparison is byte-by-byte and letter casing now
612-
* matters since lower- and upper-case letters have different
613-
* byte codes.
614-
*
615-
* The REGEXP function can't be easily made to accept two
616-
* parameters, so we'll have to use a hack to get around this.
617-
*
618-
* If the first character of the pattern is a null byte, we'll
619-
* remove it and make the comparison case-sensitive. This should
620-
* be reasonably safe since PHP does not allow null bytes in
621-
* regular expressions anyway.
622-
*/
623-
$is_binary = '' !== $pattern && "\x00" === $pattern[0];
624-
if ( $is_binary ) {
625-
$pattern = substr( $pattern, 1 );
626-
}
627610

628611
/*
629612
* Data that is not valid UTF-8 can only be a binary string in MySQL,
630613
* and MySQL matches binary strings byte by byte and case-sensitively.
631614
*/
632-
$byte_mode = $is_binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field );
615+
$byte_mode = 1 === $binary || ! $this->regexp_is_utf8( $pattern ) || ! $this->regexp_is_utf8( $field );
633616
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode );
634617
$result = preg_match( $compiled, $field );
635618
if ( false === $result ) {
@@ -1291,7 +1274,7 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
12911274
}
12921275

12931276
$newline = $unix_lines ? '(*LF)' : '(*ANY)';
1294-
$compiled = '/' . $newline . $this->regexp_escape_delimiter( $pattern ) . '/' . $modifiers;
1277+
$compiled = '/' . $newline . $this->regexp_escape_pattern( $pattern ) . '/' . $modifiers;
12951278

12961279
/*
12971280
* Compile errors are the only preg failures that emit PHP warnings.
@@ -1312,13 +1295,19 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
13121295
}
13131296

13141297
/**
1315-
* Escape pattern delimiters that are not already escaped.
1298+
* Escape pattern delimiters and NUL bytes that are not already escaped.
1299+
*
1300+
* PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00".
13161301
*
13171302
* @param string $pattern The regex pattern.
13181303
*
13191304
* @return string Pattern safe to wrap in slash delimiters.
13201305
*/
1321-
private function regexp_escape_delimiter( $pattern ) {
1306+
private function regexp_escape_pattern( $pattern ) {
1307+
$special = array(
1308+
'/' => '\\/',
1309+
"\0" => '\\x00',
1310+
);
13221311
$escaped = '';
13231312
$quoted = false;
13241313
$length = strlen( $pattern );
@@ -1329,19 +1318,20 @@ private function regexp_escape_delimiter( $pattern ) {
13291318
$escaped .= '\\E';
13301319
$quoted = false;
13311320
++$i;
1332-
} elseif ( '/' === $character ) {
1333-
$escaped .= '\\E\\/\\Q';
1321+
} elseif ( isset( $special[ $character ] ) ) {
1322+
$escaped .= '\\E' . $special[ $character ] . '\\Q';
13341323
} else {
13351324
$escaped .= $character;
13361325
}
13371326
continue;
13381327
}
13391328
if ( '\\' === $character && $i + 1 < $length ) {
1340-
$escaped .= $character . $pattern[ $i + 1 ];
1341-
$quoted = 'Q' === $pattern[ $i + 1 ];
1329+
$next = $pattern[ $i + 1 ];
1330+
$escaped .= isset( $special[ $next ] ) ? $special[ $next ] : $character . $next;
1331+
$quoted = 'Q' === $next;
13421332
++$i;
1343-
} elseif ( '/' === $character ) {
1344-
$escaped .= '\\/';
1333+
} elseif ( isset( $special[ $character ] ) ) {
1334+
$escaped .= $special[ $character ];
13451335
} else {
13461336
$escaped .= $character;
13471337
}

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,75 @@ public function testRegexpLegacyOperatorUtf8Handling() {
469469
$this->assertSame( '1', $this->last_result[0]->r );
470470
}
471471

472+
public function testRegexpNulBytePattern() {
473+
// A pattern starting with a NUL byte is an ordinary case-insensitive pattern.
474+
$this->assertQuery( 'CREATE TABLE nul_patterns (id INT PRIMARY KEY, s TEXT, p TEXT)' );
475+
$this->assertQuery(
476+
"INSERT INTO nul_patterns VALUES (1, X'610062', X'0042'), (2, 'aBc', X'0062'), (3, 'abc', 'B')"
477+
);
478+
$this->assertQuery(
479+
'SELECT
480+
s REGEXP p AS matched,
481+
s NOT REGEXP p AS not_matched,
482+
s REGEXP BINARY p AS binary_matched,
483+
REGEXP_LIKE(s, p) AS like_matched,
484+
REGEXP_INSTR(s, p) AS position
485+
FROM nul_patterns ORDER BY id'
486+
);
487+
$this->assertEquals(
488+
array(
489+
(object) array(
490+
'matched' => '1',
491+
'not_matched' => '0',
492+
'binary_matched' => '0',
493+
'like_matched' => '1',
494+
'position' => '2',
495+
),
496+
(object) array(
497+
'matched' => '0',
498+
'not_matched' => '1',
499+
'binary_matched' => '0',
500+
'like_matched' => '0',
501+
'position' => '0',
502+
),
503+
(object) array(
504+
'matched' => '1',
505+
'not_matched' => '0',
506+
'binary_matched' => '0',
507+
'like_matched' => '1',
508+
'position' => '2',
509+
),
510+
),
511+
$this->last_result
512+
);
513+
}
514+
515+
public function testRegexpLegacyOperatorPrecedence() {
516+
// REGEXP binds tighter than comparison operators.
517+
$this->assertQuery(
518+
"SELECT
519+
1 = 'a' REGEXP 'a' AS a,
520+
0 = 'a' REGEXP 'b' AS b,
521+
'a' REGEXP 'a' = 1 AS c,
522+
1 = 'a' NOT REGEXP 'b' AS d,
523+
1 + 1 REGEXP '2' AS e,
524+
NOT 'a' REGEXP 'b' AS f"
525+
);
526+
$this->assertEquals(
527+
array(
528+
(object) array(
529+
'a' => '1',
530+
'b' => '1',
531+
'c' => '1',
532+
'd' => '1',
533+
'e' => '1',
534+
'f' => '1',
535+
),
536+
),
537+
$this->last_result
538+
);
539+
}
540+
472541
public function testRegexpLegacyOperatorMatchesBinaryDataByBytes() {
473542
// Non-UTF-8 data is matched like MySQL binary strings: byte by byte and case-sensitively.
474543
$this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' );

0 commit comments

Comments
 (0)