Fix MySQL lexer edge cases - #460
Closed
JanJakes wants to merge 3 commits into
Closed
Conversation
The final backslash-stripping step used preg_replace() with the "u" (UTF-8) modifier. That modifier makes PCRE validate the whole subject as UTF-8 and return null on the first invalid byte; since get_value() is typed ": string", the null turned into a fatal TypeError. MySQL string literals may legitimately carry non-UTF-8 bytes (binary or other-charset payloads), and the lexer scans them at the byte level, so reading the value of such a literal crashed. Switch the modifier to "s" (DOTALL). A byte-wise strip is binary-safe, yields identical results for valid UTF-8 (no continuation byte is a backslash), and additionally handles a backslash preceding a newline byte.
When resolving a function keyword (SYM_FN), the lexer peeks for a following "("
and, under SQL_MODE_IGNORE_SPACE, skips intervening whitespace first. It skipped
by advancing bytes_already_read and never restored it. When no "(" followed, the
keyword was emitted as an IDENTIFIER whose length — derived from
bytes_already_read in produce() — now covered the trailing whitespace, so the
extracted value was e.g. "COUNT " instead of "COUNT". Under this ANSI-style mode
a column or table named after a function would resolve to the wrong identifier.
Peek with a local index instead of mutating bytes_already_read, so the token's
byte range ends at the keyword and the next scan consumes the whitespace.
read_mysql_comment() read at most five version digits, so a six-digit MMmmrr version comment — added in MySQL 8.4 — was misparsed: /*!100000 ... */ gated as version 10000 instead of 100000, and the sixth digit of /*!080400 ... */ leaked into the comment body as SQL. Mirror MySQL's own lexer rule (sql/sql_lex.cc): the first five characters must be digits; a sixth digit immediately followed by whitespace extends the version to six digits; otherwise the version stays five digits and any extra is content.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IGNORE_SPACElookahead whitespace outside function-name token rangesWhy
These cases currently produce incorrect token values or ranges, or fail to recognize version comments supported by current MySQL releases.
This PR is stacked on #452, which already contains the quoted-identifier backslash fix that was previously part of this branch.
Validation
composer testinpackages/mysql-parser— 142 tests, 1,225,624 assertionsgit diff --check