diff --git a/tests/WP_SQLite_Driver_Tests.php b/tests/WP_SQLite_Driver_Tests.php index 95a478db..95f31175 100644 --- a/tests/WP_SQLite_Driver_Tests.php +++ b/tests/WP_SQLite_Driver_Tests.php @@ -6138,6 +6138,14 @@ public function testSelectColumnNames(): void { $this->assertQuery( 'CREATE TABLE t (id INT, name VARCHAR(255))' ); $this->assertQuery( 'INSERT INTO t (id, name) VALUES (1, "John"), (2, "Jane")' ); + // Literal with a NULL byte (no explicit alias). + $result = $this->assertQuery( "SELECT 'abc\0def'" ); + $this->assertSame( array( 'abc' ), array_keys( (array) $result[0] ) ); + + // Literal with a NULL byte (with an explicit alias). + $result = $this->assertQuery( "SELECT 'abc\0def' AS `col1`" ); + $this->assertSame( array( 'col1' ), array_keys( (array) $result[0] ) ); + // Columns (no explicit alias). $result = $this->assertQuery( 'SELECT id, name FROM t' ); $this->assertSame( array( 'id', 'name' ), array_keys( (array) $result[0] ) ); diff --git a/wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php b/wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php index 4aaf10e4..f57814ff 100644 --- a/wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php +++ b/wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php @@ -4566,6 +4566,13 @@ public function translate_select_item( WP_Parser_Node $node ): string { $is_text_string_literal = $text_string_literal && $item === $this->translate( $text_string_literal ); if ( $is_text_string_literal ) { $alias = $text_string_literal->get_first_child_token()->get_value(); + + // When the literal value contains a NULL byte, MySQL truncates the + // resulting identifier at the position of the first one of them. + $fist_null_byte_pos = strpos( $alias, "\0" ); + if ( false !== $fist_null_byte_pos ) { + $alias = substr( $alias, 0, $fist_null_byte_pos ); + } return sprintf( '%s AS %s', $item, $this->quote_sqlite_identifier( $alias ) ); }