Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] ) );
Expand Down
7 changes: 7 additions & 0 deletions wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this matter in more places than just translate_select_item?

Copy link
Member Author

@JanJakes JanJakes Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamziel I will check that a bit deeper in a follow-up PR. There is the case where you type this explicitly (SELECT 1 AS 'a\0b'), and we need a follow-up to tackle that. Then, I think there is no other way where a string literal gets "converted" to an alias, but—I found some inconsistencies in how we process string literals vs. quoted identifiers and want to improve that too. We seem to treat them the same, but they shouldn't. It might be an interesting edge case bug going down to the lexer.

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 ) );
}

Expand Down
Loading