Skip to content

Commit bddd859

Browse files
committed
Read non-ASCII characters in names after @
The scan for the name after a single "@" stopped at the first non-ASCII byte, and the rest became a separate token. "SET @é = 1" failed to parse, and "SELECT @é" read an empty variable with the alias "é". MySQL reads these as one name, and returned NULL for "@x🐬" after "SET @x = 3". Accepting 4-byte characters in identifiers extended this to names like "@x🐬", which failed to parse before and would now read "@x" with the alias "🐬". The same scan also reads the host part of a user, as in "u@é.example.com". MySQL also takes invalid UTF-8 bytes here, as it does in identifiers. The lexer keeps rejecting them, as it already does in identifiers.
1 parent 8d00046 commit bddd859

3 files changed

Lines changed: 71 additions & 13 deletions

File tree

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,11 @@ private function produce_with_or_rollup( array &$out, int $with_start, int $with
667667
*
668668
* MySQL treats "@" as its own terminal. A single "@" is followed by the
669669
* unquoted user-variable name (always an identifier, never a keyword; it may
670-
* contain "." and "$"), by an empty name (MySQL's empty LEX_HOSTNAME, which
671-
* makes "SELECT @" and "user1@" valid), or by a quoted name supplied by the
672-
* following token. A "@@" prefix is two "@" terminals, followed by the system
673-
* variable name. As in MySQL, that name can't be empty, and only backticks
674-
* can quote it.
670+
* contain ".", "$", and non-ASCII characters), by an empty name (MySQL's empty
671+
* LEX_HOSTNAME, which makes "SELECT @" and "user1@" valid), or by a quoted name
672+
* supplied by the following token. A "@@" prefix is two "@" terminals, followed
673+
* by the system variable name. As in MySQL, that name can't be empty, and only
674+
* backticks can quote it.
675675
*
676676
* @param int $at_start Byte offset of the "@" token.
677677
*/
@@ -697,14 +697,21 @@ private function produce_at_variable( array &$out, int $at_start ): void {
697697
return;
698698
}
699699

700-
$name_length = strspn(
701-
$this->sql,
702-
self::IDENTIFIER_MASK . '.',
703-
$position
704-
);
700+
// Read the identifier characters and any "." between them as one name.
701+
while ( true ) {
702+
$this->read_identifier();
703+
if ( '.' !== ( $this->sql[ $this->bytes_already_read ] ?? null ) ) {
704+
break;
705+
}
706+
$this->bytes_already_read += 1;
707+
}
708+
709+
// The "." read as a part of the name is not a qualified name separator.
710+
$this->next_state = self::STATE_START;
711+
712+
$name_length = $this->bytes_already_read - $position;
705713
if ( $name_length > 0 ) {
706-
$out[] = $this->make_token( self::IDENT, $position, $name_length );
707-
$this->bytes_already_read = $position + $name_length;
714+
$out[] = $this->make_token( self::IDENT, $position, $name_length );
708715
} elseif ( "'" !== $next && '"' !== $next && '`' !== $next ) {
709716
$out[] = $this->make_token( self::IDENT, $position, 0 );
710717
}
@@ -1026,7 +1033,7 @@ private function get_current_token_bytes(): string {
10261033
*
10271034
* This function reads characters that are allowed in an unquoted identifier.
10281035
* It accepts digit-only sequences too; callers scan those as numbers, except
1029-
* after "." or "@@".
1036+
* after ".", "@", or "@@".
10301037
*
10311038
* Rules:
10321039
* 1. Allowed characters are ASCII a-z, A-Z, 0-9, _, $, and Unicode U+0080-U+10FFFF.

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,53 @@ public function test_at_name_splits_into_at_and_ident(): void {
217217
$this->assertSame( 4, $tokens[2]->length );
218218
}
219219

220+
/**
221+
* In MySQL, the name after "@" is one token, also with non-ASCII characters.
222+
*
223+
* @dataProvider data_at_names
224+
*/
225+
public function test_at_name_is_one_token( string $sql, array $expected ): void {
226+
$tokens = ( new WP_MySQL_Lexer( $sql ) )->remaining_tokens();
227+
$this->assertSame(
228+
$expected,
229+
array_map(
230+
static function ( WP_MySQL_Token $token ): string {
231+
return $token->get_bytes();
232+
},
233+
$tokens
234+
)
235+
);
236+
}
237+
238+
/**
239+
* @return array<string,array{0:string,1:string[]}>
240+
*/
241+
public function data_at_names(): array {
242+
return array(
243+
'ASCII' => array( '@a1_$', array( '@', 'a1_$', '', '' ) ),
244+
'with a dot' => array( '@a.b', array( '@', 'a.b', '', '' ) ),
245+
'starting with a dot' => array( '@.a', array( '@', '.a', '', '' ) ),
246+
'ending with a dot' => array( '@a.', array( '@', 'a.', '', '' ) ),
247+
'only dots' => array( '@..', array( '@', '..', '', '' ) ),
248+
'2-byte character' => array( '@é', array( '@', 'é', '', '' ) ),
249+
'after an ASCII character' => array( '@xé', array( '@', 'xé', '', '' ) ),
250+
'3-byte character' => array( '@x€y', array( '@', 'x€y', '', '' ) ),
251+
'4-byte character' => array( '@🐬', array( '@', '🐬', '', '' ) ),
252+
'4-byte character after x' => array( '@x🐬', array( '@', 'x🐬', '', '' ) ),
253+
'non-ASCII after a dot' => array( '@a.🐬', array( '@', 'a.🐬', '', '' ) ),
254+
'non-ASCII before a dot' => array( '@🐬.a', array( '@', '🐬.a', '', '' ) ),
255+
'followed by an operator' => array( '@é+1', array( '@', 'é', '+', '1', '', '' ) ),
256+
'followed by a name' => array( '@é a', array( '@', 'é', 'a', '', '' ) ),
257+
'followed by a number' => array( '@a.b .5', array( '@', 'a.b', '.5', '', '' ) ),
258+
'host part of a user' => array( 'u@é.example.com', array( 'u', '@', 'é.example.com', '', '' ) ),
259+
260+
// Invalid UTF-8 ends the stream, as elsewhere in the lexer.
261+
'invalid byte' => array( "@x\xFF", array( '@', 'x' ) ),
262+
'truncated 4-byte sequence' => array( "@x\xF0\x9F\x90", array( '@', 'x' ) ),
263+
'invalid byte first' => array( "@\xFF", array( '@', '' ) ),
264+
);
265+
}
266+
220267
public function test_at_at_splits_into_two_at_signs(): void {
221268
$this->assertSame(
222269
array( 'SELECT', 'AT_SIGN', 'AT_SIGN', 'IDENT', 'END_OF_INPUT', 'YYEOF' ),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public function test_parses_representative_statements(): void {
107107
'ALTER TABLE t ADD COLUMN c JSON',
108108
'DROP TABLE IF EXISTS t',
109109
'SET @v = 1',
110+
'SET @é = 1',
111+
'SELECT @xé + 1 AS plus_one',
112+
'SELECT @x🐬 + 1 AS plus_one, a FROM t',
113+
'CREATE USER u@é',
110114
'SET @@SESSION.sql_mode = \'\'',
111115
'SELECT @@`version`',
112116
'SHOW TABLES',

0 commit comments

Comments
 (0)