Skip to content

Malformed queries trigger PHP warnings in the parser #574

Open

Description

@JanJakes

Problem

When the lexer can't tokenize a query, the parser emits a series of PHP warnings before the driver throws Failed to parse the MySQL query. MySQL returns a single syntax error.

Reproduction

SELECT 'abc
MySQL Driver
Result Error 1064: You have an error in your SQL syntax 18 PHP warnings, then Failed to parse the MySQL query.

The driver warnings alternate between these two:

Warning: Undefined array key 1 in .../src/parser/class-wp-parser.php on line 57
Warning: Attempt to read property "id" on null in .../src/parser/class-wp-parser.php on line 57

Any input that the lexer rejects does the same, for example SELECT x'ab01xyz'.

MySQL results are from MySQL 8.4. Driver results are from #573 at cd4384bc, with PHP 8.5.12 and SQLite 3.54.1.

Users see the warnings when WP_DEBUG is on. When an error handler turns warnings into exceptions, as PHPUnit does by default, the warning replaces the driver's own exception.

Cause

WP_MySQL_Lexer::remaining_tokens() stops at the invalid input and returns the tokens it has read so far, without an EOF token. The lookahead check in WP_Parser::parse_recursive() then reads $this->tokens[ $this->position ]->id past the end of the token list.

Direction

  • Throw a syntax error when the lexer fails. For example, create_parser() could check that the lexer reached EOF and throw new_invalid_input_exception() ("MySQL query syntax error.").
  • Reject odd-length X'…' literals in the lexer. MySQL rejects quoted hex literals with an odd number of digits, such as X'711', in its lexer with error 1064. Our lexer accepts them, so the query fails only when SQLite rejects the translated literal with unrecognized token: "X'711'". Once lexer failures are handled, both lexer copies (packages/mysql-on-sqlite and packages/mysql-parser) can reject them like MySQL does. testOddLengthQuotedHexadecimalLiteralsAreRejected from Support odd-length 0x hexadecimal literals #573 can then assert the syntax error.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions