Skip to content

Commit 6a67e82

Browse files
committed
Allow comments inside skipped version comments
A version comment that doesn't apply to the MySQL version can contain other comments, one level deep. The lexer stopped at the first closing marker, so in "SELECT 1 /*!99999 /* */ */" the rest became "*" and "/", which failed one query in the corpus. https://github.com/mysql/mysql-server/blob/mysql-8.4.10/sql/sql_lex.cc#L1313
1 parent 3d11fc9 commit 6a67e82

5 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,17 @@ private function read_mysql_comment(): int {
14581458
$version = (int) substr( $this->sql, $this->bytes_already_read, $version_length );
14591459

14601460
if ( $this->mysql_version < $version ) {
1461-
// Version not satisfied. Treat the content as a regular comment.
1461+
// Version not satisfied. Treat the content as a comment. Unlike a regular
1462+
// comment, it can contain other comments, which can't be nested any further.
1463+
while ( true ) {
1464+
$nested_start = strpos( $this->sql, '/*', $this->bytes_already_read );
1465+
$comment_end = strpos( $this->sql, '*/', $this->bytes_already_read );
1466+
if ( false === $nested_start || ( false !== $comment_end && $comment_end < $nested_start ) ) {
1467+
break;
1468+
}
1469+
$this->bytes_already_read = $nested_start + 2;
1470+
$this->read_comment_content();
1471+
}
14621472
$this->read_comment_content();
14631473
return self::COMMENT;
14641474
} else {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ public function data_version_comments(): array {
149149
'six digits without space' => array( 'SELECT /*!080100+ */ 1', 80100, array( 'SELECT', '0', '+', '1' ) ),
150150
'six digits at comment end' => array( 'SELECT /*!080100*/ + 1', 80100, array( 'SELECT', '0', '+', '1' ) ),
151151
'seven digits' => array( 'SELECT /*!0801007 + */ 1', 80100, array( 'SELECT', '07', '+', '1' ) ),
152+
153+
// A version comment that doesn't apply can contain other comments.
154+
'nested comment' => array( 'SELECT 1 /*!99999 /* */ */', 80410, array( 'SELECT', '1' ) ),
155+
'text around nested' => array( 'SELECT 1 /*!99999 a /* b */ c */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
156+
'two nested comments' => array( 'SELECT 1 /*!99999 /* a */ /* b */ */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
157+
'nested version comment' => array( 'SELECT 1 /*!99999 /*!99999 a */ */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
158+
'nested comment on 5.7' => array( 'SELECT 1 /*!80000 /* */ */ + 2', 50744, array( 'SELECT', '1', '+', '2' ) ),
159+
160+
// A nested comment ends at its first closing marker.
161+
'only one level of nesting' => array( 'SELECT 1 /*!99999 /* /* */ */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
162+
'text after the outer end' => array( 'SELECT 1 /*!99999 /* /* */ */ */', 80410, array( 'SELECT', '1', '*', '/' ) ),
163+
164+
// Markers can't overlap.
165+
'"/*/" opens a comment' => array( 'SELECT 1 /*!99999 /*/ */ */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
166+
'"*/*" closes a comment' => array( 'SELECT 1 /*!99999 */* 2', 80410, array( 'SELECT', '1', '*', '2' ) ),
167+
168+
// Other comments have their own rules.
169+
'regular comment' => array( 'SELECT 1 /* /* */ + 2', 80410, array( 'SELECT', '1', '+', '2' ) ),
170+
'applied version comment' => array( 'SELECT 1 /*!80000 /* */ + 2 */', 80410, array( 'SELECT', '1', '+', '2' ) ),
152171
);
153172

154173
foreach ( array(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function test_parses_representative_statements(): void {
115115
'SELECT @@`version`',
116116
'SHOW TABLES',
117117
'EXPLAIN SELECT 1',
118+
'SELECT 1 /*!99999 /* */ */',
118119
);
119120
foreach ( $statements as $sql ) {
120121
$this->assertNotNull( self::parse( $sql ), "Failed to parse: $sql" );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class WP_MySQL_Server_Suite_Parser_Tests extends TestCase {
2020
* legitimately changes.
2121
*/
2222
const EXPECTED_QUERIES = 69076;
23-
const EXPECTED_FAILURES = 64;
23+
const EXPECTED_FAILURES = 63;
2424

2525
public function test_corpus_acceptance_rate(): void {
2626
$parser = WP_MySQL_Parser_Factory::create_parser();

‎packages/mysql-parser/tests/data/corpus-failures.csv‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ UNLOCK TABLES"
6969
"SELECT 'a%' NOT LIKE 'a!%' ESCAPE '!', 'a%' NOT LIKE 'a!' || '%' ESCAPE '!'"
7070
"SELECT 'a%' NOT LIKE 'a!%' ESCAPE '$', 'a%' NOT LIKE 'a!' || '%' ESCAPE '$'"
7171
"SELECT * from ""full"""
72-
"SELECT 1 /*!99999 /* */ */"
7372
"SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
7473
create procedure test_resignal()
7574
begin

0 commit comments

Comments
 (0)