Skip to content

Commit a6854bc

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 cf9b01b commit a6854bc

3 files changed

Lines changed: 97 additions & 47 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_regexp_string_arg( $node->get_first_child_node() );
51715171
$pattern = $this->translate_regexp_string_arg( $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 & 31 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 ),
@@ -598,44 +599,27 @@ public function isnull( $field ) {
598599
}
599600

600601
/**
601-
* Method to emulate MySQL REGEXP() function.
602+
* Method to emulate the MySQL REGEXP operator.
602603
*
603604
* @param string|null $pattern Regular expression to match.
604605
* @param string|null $field Haystack.
606+
* @param int $binary 1 for REGEXP BINARY, which matches bytes
607+
* case-sensitively; 0 otherwise.
605608
*
606609
* @throws Exception If the pattern is not a valid regular expression.
607610
* @return int|null 1 if matched, 0 if not matched, or NULL for NULL input.
608611
*/
609-
public function regexp( $pattern, $field ) {
612+
public function regexp( $pattern, $field, $binary = 0 ) {
610613
if ( null === $pattern ) {
611614
return null;
612615
}
613616
$pattern = $this->regexp_string_arg( $pattern );
614617

615-
/*
616-
* If the original query says REGEXP BINARY
617-
* the comparison is byte-by-byte and letter casing now
618-
* matters since lower- and upper-case letters have different
619-
* byte codes.
620-
*
621-
* The REGEXP function can't be easily made to accept two
622-
* parameters, so we'll have to use a hack to get around this.
623-
*
624-
* If the first character of the pattern is a null byte, we'll
625-
* remove it and make the comparison case-sensitive. This should
626-
* be reasonably safe since PHP does not allow null bytes in
627-
* regular expressions anyway.
628-
*/
629-
$is_binary = '' !== $pattern && "\x00" === $pattern[0];
630-
if ( $is_binary ) {
631-
$pattern = substr( $pattern, 1 );
632-
}
633-
634618
/*
635619
* Data that is not valid UTF-8 can only be a binary string in MySQL,
636620
* and MySQL matches binary strings byte by byte and case-sensitively.
637621
*/
638-
$byte_mode = $is_binary || ! $this->regexp_is_utf8( $pattern );
622+
$byte_mode = 1 === $binary || ! $this->regexp_is_utf8( $pattern );
639623

640624
// Like MySQL, report an invalid pattern even when the subject is NULL.
641625
$compiled = $this->regexp_compile( $pattern, $byte_mode ? 'c' : '', $byte_mode );
@@ -1375,7 +1359,7 @@ private function regexp_compile( $pattern, $match_type, $binary_mode = false ) {
13751359
}
13761360

13771361
$newline = $unix_lines ? '(*LF)' : '(*ANY)';
1378-
$compiled = '/' . $newline . $this->regexp_escape_delimiter( $pattern ) . '/' . $modifiers;
1362+
$compiled = '/' . $newline . $this->regexp_escape_pattern( $pattern ) . '/' . $modifiers;
13791363
$valid = $this->regexp_run(
13801364
function () use ( $compiled ) {
13811365
return preg_match( $compiled, '' );
@@ -1392,13 +1376,19 @@ function () use ( $compiled ) {
13921376
}
13931377

13941378
/**
1395-
* Escape pattern delimiters that are not already escaped.
1379+
* Escape pattern delimiters and NUL bytes that are not already escaped.
1380+
*
1381+
* PHP before 8.2 rejects NUL bytes in patterns, so they become "\x00".
13961382
*
13971383
* @param string $pattern The regex pattern.
13981384
*
13991385
* @return string Pattern safe to wrap in slash delimiters.
14001386
*/
1401-
private function regexp_escape_delimiter( $pattern ) {
1387+
private function regexp_escape_pattern( $pattern ) {
1388+
$special = array(
1389+
'/' => '\\/',
1390+
"\0" => '\\x00',
1391+
);
14021392
$escaped = '';
14031393
$quoted = false;
14041394
$length = strlen( $pattern );
@@ -1409,19 +1399,20 @@ private function regexp_escape_delimiter( $pattern ) {
14091399
$escaped .= '\\E';
14101400
$quoted = false;
14111401
++$i;
1412-
} elseif ( '/' === $character ) {
1413-
$escaped .= '\\E\\/\\Q';
1402+
} elseif ( isset( $special[ $character ] ) ) {
1403+
$escaped .= '\\E' . $special[ $character ] . '\\Q';
14141404
} else {
14151405
$escaped .= $character;
14161406
}
14171407
continue;
14181408
}
14191409
if ( '\\' === $character && $i + 1 < $length ) {
1420-
$escaped .= $character . $pattern[ $i + 1 ];
1421-
$quoted = 'Q' === $pattern[ $i + 1 ];
1410+
$next = $pattern[ $i + 1 ];
1411+
$escaped .= isset( $special[ $next ] ) ? $special[ $next ] : $character . $next;
1412+
$quoted = 'Q' === $next;
14221413
++$i;
1423-
} elseif ( '/' === $character ) {
1424-
$escaped .= '\\/';
1414+
} elseif ( isset( $special[ $character ] ) ) {
1415+
$escaped .= $special[ $character ];
14251416
} else {
14261417
$escaped .= $character;
14271418
}

‎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
@@ -502,6 +502,75 @@ public function testRegexpLegacyOperatorUtf8Handling() {
502502
$this->assertSame( '1', $this->last_result[0]->r );
503503
}
504504

505+
public function testRegexpNulBytePattern() {
506+
// A pattern starting with a NUL byte is an ordinary case-insensitive pattern.
507+
$this->assertQuery( 'CREATE TABLE nul_patterns (id INT PRIMARY KEY, s TEXT, p TEXT)' );
508+
$this->assertQuery(
509+
"INSERT INTO nul_patterns VALUES (1, X'610062', X'0042'), (2, 'aBc', X'0062'), (3, 'abc', 'B')"
510+
);
511+
$this->assertQuery(
512+
'SELECT
513+
s REGEXP p AS matched,
514+
s NOT REGEXP p AS not_matched,
515+
s REGEXP BINARY p AS binary_matched,
516+
REGEXP_LIKE(s, p) AS like_matched,
517+
REGEXP_INSTR(s, p) AS position
518+
FROM nul_patterns ORDER BY id'
519+
);
520+
$this->assertEquals(
521+
array(
522+
(object) array(
523+
'matched' => '1',
524+
'not_matched' => '0',
525+
'binary_matched' => '0',
526+
'like_matched' => '1',
527+
'position' => '2',
528+
),
529+
(object) array(
530+
'matched' => '0',
531+
'not_matched' => '1',
532+
'binary_matched' => '0',
533+
'like_matched' => '0',
534+
'position' => '0',
535+
),
536+
(object) array(
537+
'matched' => '1',
538+
'not_matched' => '0',
539+
'binary_matched' => '0',
540+
'like_matched' => '1',
541+
'position' => '2',
542+
),
543+
),
544+
$this->last_result
545+
);
546+
}
547+
548+
public function testRegexpLegacyOperatorPrecedence() {
549+
// REGEXP binds tighter than comparison operators.
550+
$this->assertQuery(
551+
"SELECT
552+
1 = 'a' REGEXP 'a' AS a,
553+
0 = 'a' REGEXP 'b' AS b,
554+
'a' REGEXP 'a' = 1 AS c,
555+
1 = 'a' NOT REGEXP 'b' AS d,
556+
1 + 1 REGEXP '2' AS e,
557+
NOT 'a' REGEXP 'b' AS f"
558+
);
559+
$this->assertEquals(
560+
array(
561+
(object) array(
562+
'a' => '1',
563+
'b' => '1',
564+
'c' => '1',
565+
'd' => '1',
566+
'e' => '1',
567+
'f' => '1',
568+
),
569+
),
570+
$this->last_result
571+
);
572+
}
573+
505574
public function testRegexpLegacyOperatorMatchesBinaryDataByBytes() {
506575
// Non-UTF-8 data is matched like MySQL binary strings: byte by byte and case-sensitively.
507576
$this->assertQuery( 'CREATE TABLE blobs (id INT PRIMARY KEY, b BLOB)' );

0 commit comments

Comments
 (0)