Skip to content

Commit e6c72e6

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. The driver stores CHECK and DEFAULT expressions by joining their tokens with spaces, and it parses them again on ALTER TABLE. Join "@@" and the variable name without a space, so that "CHECK (a < @@max_connections)" isn't stored as "a < @@ max_connections", which no longer parses. MySQL rejects variables in these expressions, but the driver accepts them. See: https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc
1 parent 7eef338 commit e6c72e6

7 files changed

Lines changed: 81 additions & 9 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,11 +2667,14 @@ private function read_next_token(): ?int {
26672667
$type = self::AT_AT_SIGN_SYMBOL;
26682668

26692669
// The name after it may be a keyword ("session" in "@@session.x").
2670+
// As in MySQL, it can't be empty, and only backticks can quote it.
26702671
if (
26712672
strspn( $this->sql, self::IDENTIFIER_MASK, $this->bytes_already_read, 1 ) > 0
26722673
|| ( $this->sql[ $this->bytes_already_read ] ?? '' ) > "\x7F"
26732674
) {
26742675
$this->next_state = self::STATE_IDENTIFIER_OR_KEYWORD;
2676+
} elseif ( '`' !== ( $this->sql[ $this->bytes_already_read ] ?? '' ) ) {
2677+
return null; // Invalid input.
26752678
}
26762679
} else {
26772680
/**

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,8 +3324,9 @@ private function get_value( WP_Parser_Node $node ): string {
33243324
* This is used for storing DEFAULT and CHECK expressions in the database.
33253325
*
33263326
* The current implementation is using a naive approach based on directly
3327-
* joining the original expression token bytes. This is safe, beacuase the
3328-
* original tokens must comprise a valid expression. While functionally
3327+
* joining the original expression token bytes. This is safe, because the
3328+
* original tokens must comprise a valid expression, and they are joined so
3329+
* that they are lexed the same way when parsed again. While functionally
33293330
* equivalent, it is not strictly identical to what MySQL stores, because
33303331
* MySQL normalizes and prints the expression in a specific format.
33313332
*
@@ -3349,10 +3350,11 @@ private function serialize_mysql_expression( WP_Parser_Node $node ): string {
33493350
$value = '';
33503351
$last_no_space = true;
33513352
foreach ( $node->get_descendant_tokens() as $token ) {
3352-
// Do not insert whitespace around parentheses and dots. Before '(',
3353-
// whitespace may break function calls, depending on the value of the
3354-
// "IGNORE_SPACE" SQL mode. Around '.', it changes how qualified names
3355-
// are lexed: "t . 1" has a number, and "order . id" has a keyword.
3353+
// Do not insert whitespace around parentheses and dots, and after "@@".
3354+
// Before '(', whitespace may break function calls, depending on the
3355+
// value of the "IGNORE_SPACE" SQL mode. Around '.', it changes how
3356+
// qualified names are lexed: "t . 1" has a number, and "order . id"
3357+
// has a keyword. After "@@", it leaves the variable name empty.
33563358
$no_space = WP_MySQL_Lexer::OPEN_PAR_SYMBOL === $token->id
33573359
|| WP_MySQL_Lexer::CLOSE_PAR_SYMBOL === $token->id
33583360
|| WP_MySQL_Lexer::DOT_SYMBOL === $token->id;
@@ -3361,7 +3363,7 @@ private function serialize_mysql_expression( WP_Parser_Node $node ): string {
33613363
$value .= ' ';
33623364
}
33633365
$value .= $token->get_bytes();
3364-
$last_no_space = $no_space;
3366+
$last_no_space = $no_space || WP_MySQL_Lexer::AT_AT_SIGN_SYMBOL === $token->id;
33653367
}
33663368
return $value;
33673369
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14547,6 +14547,29 @@ public function testCheckConstraintsWithQualifiedNames(): void {
1454714547
$this->assertQuery( 'INSERT INTO `order` VALUES (1, 10)' );
1454814548
}
1454914549

14550+
public function testCheckConstraintsAndDefaultsWithSystemVariables(): void {
14551+
// MySQL rejects variables in CHECK and DEFAULT expressions, but the driver
14552+
// accepts them. ALTER TABLE parses the stored expressions again.
14553+
$this->assertQuery( 'CREATE TABLE t (a INT DEFAULT (@@auto_increment_increment), b INT, CHECK (b < @@max_connections))' );
14554+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN c INT' );
14555+
14556+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
14557+
$this->assertEquals(
14558+
implode(
14559+
"\n",
14560+
array(
14561+
'CREATE TABLE `t` (',
14562+
' `a` int DEFAULT (@@auto_increment_increment),',
14563+
' `b` int DEFAULT NULL,',
14564+
' `c` int DEFAULT NULL,',
14565+
' CONSTRAINT `t_chk_1` CHECK (b < @@max_connections)',
14566+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
14567+
)
14568+
),
14569+
$result[0]->{'Create Table'}
14570+
);
14571+
}
14572+
1455014573
public function testWriteWithUsageOfInformationSchemaTables(): void {
1455114574
// Ensure "information_schema.tables" is empty.
1455214575
$this->assertQuery( 'DROP TABLE _options, _dates' );

‎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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,9 @@ private function produce_with_or_rollup( array &$out, int $with_start, int $with
688688
* unquoted user-variable name (always an identifier, never a keyword; it may
689689
* contain "." and "$"), by an empty name (MySQL's empty LEX_HOSTNAME, which
690690
* makes "SELECT @" and "user1@" valid), or by a quoted name supplied by the
691-
* following token. A "@@" prefix is two "@" terminals, with the system
692-
* variable name scanned afterwards in the STATE_IDENTIFIER_OR_KEYWORD state.
691+
* following token. A "@@" prefix is two "@" terminals, followed by the system
692+
* variable name. As in MySQL, that name can't be empty, and only backticks
693+
* can quote it.
693694
*
694695
* @param int $at_start Byte offset of the "@" token.
695696
*/
@@ -709,6 +710,8 @@ private function produce_at_variable( array &$out, int $at_start ): void {
709710
|| ( $this->sql[ $this->bytes_already_read ] ?? '' ) > "\x7F"
710711
) {
711712
$this->next_state = self::STATE_IDENTIFIER_OR_KEYWORD;
713+
} elseif ( '`' !== ( $this->sql[ $this->bytes_already_read ] ?? '' ) ) {
714+
$this->stream_ended = true; // Invalid input.
712715
}
713716
return;
714717
}

‎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)