Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

digester: avoid infinite loop when an invalid token is seen #313

Merged
merged 1 commit into from
May 5, 2019

Conversation

kennytm
Copy link
Contributor

@kennytm kennytm commented May 1, 2019

What problem does this PR solve?

Starting from pingcap/tidb#10284, executing the following would cause TiDB to enter an infinite loop:

create table `🥳`(a int);
select * from `🥳`;         -- ← this line

This is caused by multiple issues:

  1. MySQL does not permit non-BMP characters like 🥳 in an identifier, no matter quoted or not. But TiDB's and MySQL's behavior differ on such non-BMP characters.

    • MySQL allows both unquoted 🥳 and quoted `🥳`, but both will be translated into a question mark `?`.
    • TiDB treats unquoted 🥳 a lexer error, but accepts the quoted `🥳` as-is.
  2. The parser.Normalize strips away the backquotes around the identifier, while MySQL adds the backquotes in all cases.

  3. Since *: support select/ explain select using bind info tidb#10284, when we execute a SELECT statement, it will call GetBindRecord. The problem is GetBindRecord calls parser.DigestHash on a normalized SQL (*: support "add session binding" tidb#10247), so we will double-normalize the statement before computing the hash.

So, up till now, the statement SELECT * FROM `🥳`; is (1) accepted by the parser, (2) normalized into SELECT * FROM 🥳;, and (3) normalized again before calculating the hash.

  1. The unquoted 🥳 is treated as a lexer error. The lexer will just return the invalid token without advancing the cursor. This throws the normalize function into an infinite loop.

What is changed and how it works?

Break the infinite loop if a lexer error is encountered, so at least running SELECT * FROM `🥳`; won't DoS the server.

We could think of how to fix the other 3 problems in other PRs.

Check List

Tests

  • Unit test

Code changes

Side effects

Related changes

@kennytm kennytm requested a review from lysu May 1, 2019 21:55
@kennytm
Copy link
Contributor Author

kennytm commented May 5, 2019

PTAL @lysu

Copy link
Collaborator

@lysu lysu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jackysp jackysp merged commit 4542e96 into master May 5, 2019
@kennytm kennytm deleted the kennytm/fix-infinite-loop-in-digest-hash branch May 5, 2019 09:30
tiancaiamao pushed a commit to tiancaiamao/parser that referenced this pull request Apr 27, 2021
lyonzhi pushed a commit to lyonzhi/parser that referenced this pull request Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants