Skip to content

Commit cd4384b

Browse files
committed
Pad odd-length hexadecimal literals
Add a leading zero to odd-length 0x literals before translating them to SQLite blob literals. Preserve leading zeros and keep rejecting odd-length quoted hexadecimal literals. Cover SELECT, HEX(), INSERT, UPDATE, and literal edge cases. Fixes #543
1 parent 0a7c269 commit cd4384b

3 files changed

Lines changed: 90 additions & 8 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4541,7 +4541,13 @@ private function translate_token( WP_MySQL_Token $token ): ?string {
45414541
*/
45424542
$value = $token->get_value();
45434543
if ( '0' === $value[0] && 'x' === $value[1] ) {
4544-
return sprintf( "x'%s'", substr( $value, 2 ) );
4544+
$hex = substr( $value, 2 );
4545+
4546+
// MySQL pads odd-length 0x literals with a leading zero.
4547+
if ( 1 === strlen( $hex ) % 2 ) {
4548+
$hex = '0' . $hex;
4549+
}
4550+
return sprintf( "x'%s'", $hex );
45454551
}
45464552
return $value;
45474553
case WP_MySQL_Lexer::AUTO_INCREMENT_SYMBOL:

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

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11153,15 +11153,81 @@ public function testBinaryLiterals(): void {
1115311153
$this->assertEquals( array( (object) array( '0b000000001' => pack( 'H*', '0001' ) ) ), $result );
1115411154
}
1115511155

11156-
public function testHexadecimalLiterals(): void {
11157-
$result = $this->assertQuery( 'SELECT 0x417a' );
11158-
$this->assertEquals( array( (object) array( '0x417a' => 'Az' ) ), $result );
11156+
/**
11157+
* @dataProvider hexadecimalLiterals
11158+
*/
11159+
public function testHexadecimalLiterals( string $literal, string $hex ): void {
11160+
$result = $this->assertQuery( "SELECT $literal" );
11161+
$this->assertEquals( array( (object) array( $literal => hex2bin( $hex ) ) ), $result );
11162+
11163+
$result = $this->assertQuery( "SELECT HEX($literal) AS value" );
11164+
$this->assertSame( $hex, $result[0]->value );
11165+
}
1115911166

11160-
$result = $this->assertQuery( "SELECT x'417a'" );
11161-
$this->assertEquals( array( (object) array( "x'417a'" => 'Az' ) ), $result );
11167+
public static function hexadecimalLiterals(): array {
11168+
return array(
11169+
array( '0x0', '00' ),
11170+
array( '0x1', '01' ),
11171+
array( '0xF', '0F' ),
11172+
array( '0x711', '0711' ),
11173+
array( '0xaBc', '0ABC' ),
11174+
array( '0x0711', '0711' ),
11175+
array( '0x00711', '000711' ),
11176+
array( '0x000711', '000711' ),
11177+
array( '0x000', '0000' ),
11178+
array( '0x123456789abcdef01', '0123456789ABCDEF01' ),
11179+
array( '0x417a', '417A' ),
11180+
array( "x'417a'", '417A' ),
11181+
array( "X'417a'", '417A' ),
11182+
array( "x''", '' ),
11183+
array( "X''", '' ),
11184+
);
11185+
}
1116211186

11163-
$result = $this->assertQuery( "SELECT X'417a'" );
11164-
$this->assertEquals( array( (object) array( "X'417a'" => 'Az' ) ), $result );
11187+
public function testOddLengthHexadecimalLiteralsInWrites(): void {
11188+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, title TEXT, name VARCHAR(200), data BLOB)' );
11189+
$this->assertQuery( 'INSERT INTO t (id, title, name, data) VALUES (51701, 0x711, 0x711, 0x711)' );
11190+
$this->assertEquals(
11191+
array(
11192+
(object) array(
11193+
'id' => '51701',
11194+
'title' => '0711',
11195+
'name' => '0711',
11196+
'data' => '0711',
11197+
),
11198+
),
11199+
$this->assertQuery( 'SELECT id, HEX(title) AS title, HEX(name) AS name, HEX(data) AS data FROM t' )
11200+
);
11201+
11202+
$this->assertQuery( 'UPDATE t SET title = 0x00711, name = 0x00711, data = 0x00711 WHERE id = 51701' );
11203+
$this->assertEquals(
11204+
array(
11205+
(object) array(
11206+
'id' => '51701',
11207+
'title' => '000711',
11208+
'name' => '000711',
11209+
'data' => '000711',
11210+
),
11211+
),
11212+
$this->assertQuery( 'SELECT id, HEX(title) AS title, HEX(name) AS name, HEX(data) AS data FROM t' )
11213+
);
11214+
}
11215+
11216+
/**
11217+
* @dataProvider oddLengthQuotedHexadecimalLiterals
11218+
*/
11219+
public function testOddLengthQuotedHexadecimalLiteralsAreRejected( string $literal ): void {
11220+
$this->expectException( WP_MySQL_On_SQLite_Exception::class );
11221+
$this->assertQuery( "SELECT $literal" );
11222+
}
11223+
11224+
public static function oddLengthQuotedHexadecimalLiterals(): array {
11225+
return array(
11226+
array( "x'1'" ),
11227+
array( "X'1'" ),
11228+
array( "x'711'" ),
11229+
array( "X'711'" ),
11230+
);
1116511231
}
1116611232

1116711233
public function testColumnInfo(): void {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,16 @@ public function testHexadecimalLiterals(): void {
17421742
"SELECT x'417a' AS `0x417a`",
17431743
'SELECT 0x417a'
17441744
);
1745+
1746+
// Odd-length 0x... values need a leading zero to form full bytes.
1747+
$this->assertQuery(
1748+
"SELECT x'0711' AS `0x711`",
1749+
'SELECT 0x711'
1750+
);
1751+
$this->assertQuery(
1752+
"SELECT x'000711' AS `0x00711`",
1753+
'SELECT 0x00711'
1754+
);
17451755
}
17461756

17471757
public function testSystemVariables(): void {

0 commit comments

Comments
 (0)