Skip to content

Commit 8cd2a07

Browse files
committed
Preserve bytes in LIKE BINARY patterns
Remove UTF-8 validation from LIKE-to-GLOB pattern conversion so raw bytes do not turn the pattern into NULL. Use DOTALL to also unescape backslashes followed by newlines. Cover byte preservation, malformed UTF-8, Unicode, wildcards, escapes, and NULL in helper and query tests. Document the remaining GLOB limitation: distinct invalid UTF-8 sequences can match as equal.
1 parent b44ddd9 commit 8cd2a07

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,8 @@ private function translate_like( WP_Parser_Node $node ): string {
49434943
$is_binary = isset( $tokens[1] ) && WP_MySQL_Lexer::BINARY_SYMBOL === $tokens[1]->id;
49444944

49454945
if ( true === $is_binary ) {
4946+
// TODO: GLOB can match different invalid UTF-8 bytes as equal.
4947+
// Use byte-wise matching for full LIKE BINARY compatibility.
49464948
$children = $node->get_children();
49474949
return sprintf(
49484950
'GLOB _helper_like_to_glob_pattern(%s)',

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,8 @@ public function reverse( $str ) {
950950
* affect also other patterns than just "LIKE BINARY". We should
951951
* consider applying some of the conversions more broadly.
952952
*
953-
* @param string $pattern
954-
* @return string
953+
* @param string|null $pattern
954+
* @return string|null
955955
*/
956956
public function _helper_like_to_glob_pattern( $pattern ) {
957957
if ( null === $pattern ) {
@@ -991,10 +991,9 @@ public function _helper_like_to_glob_pattern( $pattern ) {
991991
* This is true also for multi-byte characters:
992992
* SELECT '\\©' prints '\©', but LIKE '\\©' is equivalent to LIKE '©'.
993993
*
994-
* However, the multi-byte behavior is likely to depend on the charset.
995-
* For now, we'll assume UTF-8 and thus the "u" modifier for the regex.
994+
* Use "s" to include escaped newlines and omit "u" to preserve raw bytes.
996995
*/
997-
$pattern = preg_replace( '/\\\\(.)/u', '$1', $pattern );
996+
$pattern = preg_replace( '/\\\\(.)/s', '$1', $pattern );
998997

999998
return $pattern;
1000999
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,63 @@ public function testTranslateLikeBinary() {
48754875
$this->assertCount( 2, $result ); // Should match both 'first' and 'FIRST'
48764876
}
48774877

4878+
/**
4879+
* @dataProvider dataLikePatterns
4880+
*/
4881+
public function testLikePatternPreservesBytes( $pattern, $expected ): void {
4882+
$functions = new WP_SQLite_PDO_User_Defined_Functions();
4883+
4884+
$this->assertSame( $expected, $functions->_helper_like_to_glob_pattern( $pattern ) );
4885+
}
4886+
4887+
public function dataLikePatterns(): array {
4888+
return array(
4889+
'null' => array( null, null ),
4890+
'empty' => array( '', '' ),
4891+
'raw bytes' => array( "\xff\xfe\x80", "\xff\xfe\x80" ),
4892+
'escaped byte' => array( "\\\xff", "\xff" ),
4893+
'truncated UTF-8' => array( "\xe2\x82", "\xe2\x82" ),
4894+
'overlong UTF-8' => array( "\xc0\xaf", "\xc0\xaf" ),
4895+
'surrogate' => array( "\xed\xa0\x80", "\xed\xa0\x80" ),
4896+
'escaped newline' => array( "\\\n", "\n" ),
4897+
'escaped CRLF' => array( "\\\r\\\n", "\r\n" ),
4898+
'escaped null byte' => array( "\\\0", "\0" ),
4899+
'escaped Unicode' => array( '\\©\\🙂', '©🙂' ),
4900+
'wildcards with bytes' => array( "\xff%_\xfe", "\xff*?\xfe" ),
4901+
'escaped wildcards' => array( "\xff\\%\\_", "\xff%_" ),
4902+
'escaped backslash' => array( "\xff\\\\", "\xff\\" ),
4903+
'GLOB metacharacters' => array( "\xff*?]", "\xff[*][?][]]" ),
4904+
);
4905+
}
4906+
4907+
/**
4908+
* @dataProvider dataLikeBinaryPatterns
4909+
*/
4910+
public function testLikeBinaryPreservesPatternBytes( $value, $pattern, $expected ): void {
4911+
$query = sprintf(
4912+
'SELECT %s LIKE BINARY %s',
4913+
null === $value ? 'NULL' : $this->engine->quote( $value ),
4914+
null === $pattern ? 'NULL' : $this->engine->quote( $pattern )
4915+
);
4916+
4917+
$this->assertSame( $expected, $this->engine->query( $query )->fetchColumn() );
4918+
}
4919+
4920+
public function dataLikeBinaryPatterns(): array {
4921+
return array(
4922+
'raw byte' => array( "\xff", "\xff", '1' ),
4923+
'escaped byte' => array( "\xff", "\\\xff", '1' ),
4924+
'nonmatching text' => array( "\xffa", "\xffb", '0' ),
4925+
'percent wildcard' => array( "\xfftail", "\xff%", '1' ),
4926+
'underscore' => array( "\xffx", "\xff_", '1' ),
4927+
'escaped wildcards' => array( "\xff%_", "\xff\\%\\_", '1' ),
4928+
'escaped newline' => array( "\n", "\\\n", '1' ),
4929+
'escaped Unicode' => array( '©🙂', '\\©\\🙂', '1' ),
4930+
'null pattern' => array( 'x', null, null ),
4931+
'null value' => array( null, 'x', null ),
4932+
);
4933+
}
4934+
48784935
public function testUniqueConstraints() {
48794936
$this->assertQuery(
48804937
"CREATE TABLE _tmp_table (

0 commit comments

Comments
 (0)