Skip to content

Commit 5cd508e

Browse files
committed
Accept 4-byte UTF-8 characters in unquoted identifiers
The lexer accepted only U+0080-U+FFFF in unquoted identifiers, as the MySQL documentation lists. But the MySQL lexer accepts any multibyte character of the connection character set, so with utf8mb4, it also accepts names like 🐬 or 𩸽. This was checked in the MySQL 5.5 and 5.7 lexer sources and on MySQL 8.4. MySQL then converts names to utf8mb3, which can't store these characters. A quoted alias like '🐬' fails since MySQL 8.0.17, but unquoted names become "?", so tables 🐬 and 😀 would collide. The lexer follows the MySQL syntax and leaves this to the code that uses the parser. See: https://bugs.mysql.com/bug.php?id=93626
1 parent f05c1f9 commit 5cd508e

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ private function read_next_token(): ?int {
803803

804804
// Fast path for keywords and identifiers.
805805
// `$byte > "\x7F"` catches any non-ASCII byte (0x80-0xFF); read_identifier()
806-
// restricts the accepted identifier codepoints to U+0080-U+FFFF.
806+
// restricts the accepted identifier codepoints to U+0080-U+10FFFF.
807807
// `"'" !== $next_byte` defers x'..', n'..' and similar special
808808
// literals to their dedicated branches below; only single quotes
809809
// form those, regardless of SQL mode.
@@ -1029,7 +1029,10 @@ private function get_current_token_bytes(): string {
10291029
* after "." or "@@".
10301030
*
10311031
* Rules:
1032-
* 1. Allowed characters are ASCII a-z, A-Z, 0-9, _, $, and Unicode U+0080-U+FFFF.
1032+
* 1. Allowed characters are ASCII a-z, A-Z, 0-9, _, $, and Unicode U+0080-U+10FFFF.
1033+
* The MySQL documentation lists only U+0080-U+FFFF, but the MySQL lexer accepts
1034+
* any utf8mb4 character, including U+10000-U+10FFFF. MySQL then stores
1035+
* these characters as "?", because it keeps names in utf8mb3.
10331036
* 2. Unquoted identifiers may begin with a digit but may not consist solely of digits.
10341037
* An exception is a part of a qualified name, such as "1" in "t.1".
10351038
*
@@ -1047,13 +1050,13 @@ private function read_identifier(): ?int {
10471050
);
10481051

10491052
// Check if the following byte can be part of a multibyte character
1050-
// in the range of U+0080 to U+FFFF before looking at further bytes.
1053+
// in the range of U+0080 to U+10FFFF before looking at further bytes.
10511054
// If it can't, bail out early to avoid unnecessary UTF-8 decoding.
10521055
// Identifiers are usually ASCII-only, so we can optimize for that.
10531056
$byte_1 = ord(
10541057
$this->sql[ $this->bytes_already_read ] ?? "\0"
10551058
);
1056-
if ( $byte_1 < 0xC2 || $byte_1 > 0xEF ) {
1059+
if ( $byte_1 < 0xC2 || $byte_1 > 0xF4 ) {
10571060
break;
10581061
}
10591062

@@ -1086,6 +1089,24 @@ private function read_identifier(): ?int {
10861089
continue;
10871090
}
10881091

1092+
// Look for a valid 4-byte UTF-8 symbol in range U+10000 - U+10FFFF.
1093+
$byte_4 = ord(
1094+
$this->sql[ $this->bytes_already_read + 3 ] ?? "\0"
1095+
);
1096+
if (
1097+
$byte_1 >= 0xF0
1098+
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1099+
&& $byte_3 >= 0x80 && $byte_3 <= 0xBF
1100+
&& $byte_4 >= 0x80 && $byte_4 <= 0xBF
1101+
// Exclude overlong encodings:
1102+
&& ! ( 0xF0 === $byte_1 && $byte_2 < 0x90 )
1103+
// Exclude codepoints above U+10FFFF:
1104+
&& ! ( 0xF4 === $byte_1 && $byte_2 > 0x8F )
1105+
) {
1106+
$this->bytes_already_read += 4;
1107+
continue;
1108+
}
1109+
10891110
// Not a valid identifier character.
10901111
break;
10911112
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,61 @@ public function test_identifier_utf8_three_byte_sequences(): void {
425425
}
426426
}
427427

428+
/**
429+
* Test valid and invalid 4-byte UTF-8 sequences in an identifier.
430+
* The validity is checked against PCRE with the "u" (PCRE_UTF8) modifier set.
431+
*
432+
* Testing all 4-byte sequences would take too long, so the first two bytes
433+
* are tested with valid trailing bytes, and the last two bytes are tested
434+
* with valid leading bytes. The first byte starts from 0xF0 to mark the
435+
* beginning of a 4-byte sequence, and the other bytes go from 128 to 255.
436+
*/
437+
public function test_identifier_utf8_four_byte_sequences(): void {
438+
$values = array();
439+
for ( $byte_1 = 0xF0; $byte_1 <= 0xFF; $byte_1 += 1 ) {
440+
for ( $byte_2 = 128; $byte_2 <= 255; $byte_2 += 1 ) {
441+
$values[] = chr( $byte_1 ) . chr( $byte_2 ) . "\x80\x80";
442+
}
443+
}
444+
for ( $byte_3 = 128; $byte_3 <= 255; $byte_3 += 1 ) {
445+
for ( $byte_4 = 128; $byte_4 <= 255; $byte_4 += 1 ) {
446+
$values[] = "\xF0\x9F" . chr( $byte_3 ) . chr( $byte_4 );
447+
}
448+
}
449+
450+
foreach ( $values as $value ) {
451+
$lexer = new WP_MySQL_Lexer( $value );
452+
$result = $lexer->next_token();
453+
$token = $lexer->get_token();
454+
455+
$is_valid = preg_match( '/^[\x{10000}-\x{10ffff}]$/u', $value );
456+
if ( $is_valid ) {
457+
$this->assertTrue( $result );
458+
$this->assertSame( WP_MySQL_Lexer::IDENT, $token->id );
459+
$this->assertSame( $value, $token->get_bytes() );
460+
} else {
461+
$this->assertFalse( $result );
462+
$this->assertNull( $token );
463+
}
464+
}
465+
}
466+
467+
public function test_identifier_with_4_byte_characters(): void {
468+
// The MySQL lexer accepts 4-byte characters in unquoted identifiers on all versions.
469+
foreach ( array( 50744, 80410 ) as $mysql_version ) {
470+
$tokens = ( new WP_MySQL_Lexer( 'SELECT a🐬b, 𩸽 FROM 🐬', $mysql_version ) )->remaining_tokens();
471+
$this->assertSame(
472+
array( 'SELECT', 'a🐬b', ',', '𩸽', 'FROM', '🐬', '', '' ),
473+
array_map(
474+
static function ( WP_MySQL_Token $token ): string {
475+
return $token->get_bytes();
476+
},
477+
$tokens
478+
)
479+
);
480+
}
481+
}
482+
428483
/**
429484
* A charset-introducer-like name used as a qualified member (after a dot)
430485
* must lex as an identifier. A real charset introducer only appears before

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public function test_parses_representative_statements(): void {
102102
'UPDATE t SET a = a + 1 WHERE b IN (SELECT b FROM u)',
103103
'DELETE FROM t WHERE a IS NOT NULL',
104104
'CREATE TABLE t (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))',
105+
'CREATE TABLE 🐬 (🐬 INT, a🐬b INT)',
106+
'SELECT 🐬.a🐬b FROM 🐬 AS 𩸽',
105107
'ALTER TABLE t ADD COLUMN c JSON',
106108
'DROP TABLE IF EXISTS t',
107109
'SET @v = 1',

0 commit comments

Comments
 (0)