Skip to content

Commit 88fedea

Browse files
committed
Reject an empty system variable name after "@@"
MySQL reads the name after "@@" in its MY_LEX_IDENT_OR_KEYWORD state, which fails on an empty name. Only a backtick skips that state. So "SELECT @@ version" and "SELECT @@'version'" are syntax errors in MySQL, while "SELECT @@`version`" works. Both lexers accepted all of them. Treat "@@" followed by neither a name character nor a backtick as invalid input, as MySQL does. See: https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc
1 parent dbcffbd commit 88fedea

5 files changed

Lines changed: 49 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2669,12 +2669,15 @@ private function read_next_token(): ?int {
26692669
$this->bytes_already_read += 1; // Consume the second '@'.
26702670
$type = self::AT_AT_SIGN_SYMBOL;
26712671

2672-
// The system variable name after it is read with keywords.
2672+
// The name after it may be a keyword ("session" in "@@session.x").
2673+
// As in MySQL, it can't be empty, and only backticks can quote it.
26732674
if (
26742675
strspn( $this->sql, self::IDENTIFIER_MASK, $this->bytes_already_read, 1 ) > 0
26752676
|| ( $this->sql[ $this->bytes_already_read ] ?? '' ) > "\x7F"
26762677
) {
26772678
$this->next_state = self::STATE_IDENTIFIER_OR_KEYWORD;
2679+
} elseif ( '`' !== ( $this->sql[ $this->bytes_already_read ] ?? '' ) ) {
2680+
return null; // Invalid input.
26782681
}
26792682
} else {
26802683
/**

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public function test_tokenize_invalid_input(): void {
5555
$this->assertNull( $lexer->get_token() );
5656
}
5757

58+
public function test_system_variable_name_must_not_be_empty(): void {
59+
// As in MySQL, the name after "@@" can't be empty, and only backticks
60+
// can quote it. Otherwise, the tokens end before the "@@", with no EOF.
61+
$invalid = array(
62+
array( 'SELECT @@', array() ),
63+
array( 'SELECT @@ version', array() ),
64+
array( "SELECT @@'version'", array() ),
65+
array( 'SELECT @@"version"', array() ),
66+
array( 'SELECT @@"version"', array( 'ANSI_QUOTES' ) ),
67+
);
68+
foreach ( $invalid as list( $sql, $sql_modes ) ) {
69+
$tokens = ( new WP_MySQL_Lexer( $sql, 80038, $sql_modes ) )->remaining_tokens();
70+
$this->assertCount( 1, $tokens, $sql );
71+
$this->assertSame( WP_MySQL_Lexer::SELECT_SYMBOL, $tokens[0]->id, $sql );
72+
}
73+
74+
$names = array();
75+
foreach ( ( new WP_MySQL_Lexer( 'SELECT @@`version`' ) )->remaining_tokens() as $token ) {
76+
$names[] = $token->get_name();
77+
}
78+
$this->assertSame( array( 'SELECT_SYMBOL', 'AT_AT_SIGN_SYMBOL', 'BACK_TICK_QUOTED_ID', 'EOF' ), $names );
79+
}
80+
5881
/**
5982
* Test that the whole U+0080 to U+FFFF UTF-8 range is valid in an identifier.
6083
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.

‎packages/mysql-parser/src/class-wp-mysql-lexer.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ private function produce_with_or_rollup( array &$out, int $with_start, int $with
695695
* makes "SELECT @" and "user1@" valid), or by a quoted name supplied by the
696696
* following token. A "@@" prefix is two "@" terminals, with the system
697697
* variable name scanned afterwards in the STATE_IDENTIFIER_OR_KEYWORD state.
698+
* As in MySQL, that name can't be empty, and only backticks can quote it.
698699
*
699700
* @param int $at_start Byte offset of the "@" token.
700701
*/
@@ -708,12 +709,14 @@ private function produce_at_variable( array &$out, int $at_start ): void {
708709
$out[] = $this->make_token( self::AT_SIGN_SYMBOL, $position, 1 );
709710
$this->bytes_already_read = $position + 1;
710711

711-
// The system variable name after it is read with keywords.
712+
// The name after it may be a keyword ("session" in "@@session.x").
712713
if (
713714
strspn( $this->sql, self::IDENTIFIER_MASK, $this->bytes_already_read, 1 ) > 0
714715
|| ( $this->sql[ $this->bytes_already_read ] ?? '' ) > "\x7F"
715716
) {
716717
$this->next_state = self::STATE_IDENTIFIER_OR_KEYWORD;
718+
} elseif ( '`' !== ( $this->sql[ $this->bytes_already_read ] ?? '' ) ) {
719+
$this->stream_ended = true; // Invalid input.
717720
}
718721
return;
719722
}

‎packages/mysql-parser/tests/WP_MySQL_Lexer_Tests.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,21 @@ public function test_invalid_input_returns_partial_stream_without_end_markers():
267267
$this->assertSame( array( 'SELECT', 'INT_NUMBER', 'WITH' ), $names );
268268
}
269269

270+
public function test_system_variable_name_must_not_be_empty(): void {
271+
// As in MySQL, the name after "@@" can't be empty, and only backticks
272+
// can quote it. Otherwise, the stream ends after the "@@".
273+
$at_at = array( 'AT_SIGN_SYMBOL', 'AT_SIGN_SYMBOL' );
274+
$this->assertSame( $at_at, self::token_names( '@@' ) );
275+
$this->assertSame( $at_at, self::token_names( '@@ version' ) );
276+
$this->assertSame( $at_at, self::token_names( "@@'version'" ) );
277+
$this->assertSame( $at_at, self::token_names( '@@"version"' ) );
278+
$this->assertSame( $at_at, self::token_names( '@@"version"', array( 'ANSI_QUOTES' ) ) );
279+
$this->assertSame(
280+
array_merge( $at_at, array( 'BACK_TICK_QUOTED_ID', 'END_OF_INPUT', 'END_MARKER' ) ),
281+
self::token_names( '@@`version`' )
282+
);
283+
}
284+
270285
public function test_high_not_precedence_emits_not2(): void {
271286
$names = self::token_names( 'SELECT NOT 1', array( 'HIGH_NOT_PRECEDENCE' ) );
272287
$this->assertContains( 'NOT2_SYMBOL', $names );

‎packages/mysql-parser/tests/WP_MySQL_Parser_Tests.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function test_parses_representative_statements(): void {
106106
'DROP TABLE IF EXISTS t',
107107
'SET @v = 1',
108108
'SET @@SESSION.sql_mode = \'\'',
109+
'SELECT @@`version`',
109110
'SHOW TABLES',
110111
'EXPLAIN SELECT 1',
111112
);
@@ -124,6 +125,8 @@ public function test_rejects_statements_invalid_in_mysql_84(): void {
124125
'SELECT.1', // "SELECT" before ".1" is a name.
125126
'SELECT order.* FROM t AS `order`', // A reserved word before ".*".
126127
'SELECT 0.select', // "SELECT" after "0." is a keyword.
128+
'SELECT @@ version', // An empty name after "@@".
129+
"SELECT @@'version'", // A single-quoted name after "@@".
127130
);
128131
foreach ( $statements as $sql ) {
129132
$this->assertNull( self::parse( $sql ), "Should not parse: $sql" );

0 commit comments

Comments
 (0)