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

expression: add check for utf8 charset when decode to avoid invalid character #29765

Closed
wants to merge 6 commits into from

Conversation

Defined2014
Copy link
Contributor

@Defined2014 Defined2014 commented Nov 15, 2021

What problem does this PR solve?

Issue Number: close #29685, close #29735

Problem Summary: check the binary string when decode them into utf8

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

check the binary string when convert them to utf8

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has not been approved.

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 15, 2021
@Defined2014
Copy link
Contributor Author

/cc @xiongjiwei @tangenta @zimulala @wjhuang2016
PTAL

@Defined2014 Defined2014 changed the title expression: add check for utf8 charset when decode to avoid invalid utf8 character expression: add check for utf8 charset when decode to avoid invalid character Nov 15, 2021
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 15, 2021
Copy link
Contributor

@xiongjiwei xiongjiwei left a comment

Choose a reason for hiding this comment

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

is it affected by sql_mode?

@Defined2014
Copy link
Contributor Author

is it affected by sql_mode?

Sql_mode = "", the result is below

mysql> SET @@sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> select char(0xe240 using utf8);
+-------------------------+
| char(0xe240 using utf8) |
+-------------------------+
|                         |
+-------------------------+
1 row in set, 2 warnings (0.00 sec)

sql_mode=default, the result is below

mysql> select char(0xe240 using utf8);
+-------------------------+
| char(0xe240 using utf8) |
+-------------------------+
| NULL                    |
+-------------------------+
1 row in set, 2 warnings (0.00 sec)

Seems like sql-mode only decide return NULL or not. @xiongjiwei

// Decode convert bytes from a specific charset to utf-8 charset.
func (e *Encoding) Decode(dest, src []byte) ([]byte, error) {
if e.name == "utf-8" {
Copy link
Member

Choose a reason for hiding this comment

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

Please use a const variable.

// we should convert target into utf8 internal.
exprInternal, _, _ := transform.String(encoding.NewDecoder(), target)
return exprInternal, false, nil
return target, false, nil
}
if types.IsBinaryStr(b.tp) {
enc := charset.NewEncoding(b.args[0].GetType().Charset)
expr, err = enc.EncodeString(expr)
Copy link
Member

Choose a reason for hiding this comment

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

Is it ok to return err directly?

Copy link
Contributor Author

@Defined2014 Defined2014 Nov 18, 2021

Choose a reason for hiding this comment

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

I think it's fine. But not sure. PTAL @xiongjiwei

@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 18, 2021
@ti-chi-bot
Copy link
Member

@Defined2014: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 18, 2021
@@ -140,8 +141,40 @@ func (e *Encoding) EncodeInternal(dest, src []byte) []byte {
return dest
}

// validUTF8 checks whether there are illegal utf8 characters in []byte.
func (e *Encoding) validUTF8(src []byte) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

there is utf8.Valid(), we can avoid write it by ourself

Copy link
Contributor Author

@Defined2014 Defined2014 Nov 22, 2021

Choose a reason for hiding this comment

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

Implement this func by ourself is for compatible err msg.

// Decode convert bytes from a specific charset to utf-8 charset.
func (e *Encoding) Decode(dest, src []byte) ([]byte, error) {
if e.name == encodings[CharsetUTF8].name {
Copy link
Contributor

Choose a reason for hiding this comment

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

@tangenta PTAL, Do we need to check the utf8 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason is #29685

@@ -150,6 +183,9 @@ func (e *Encoding) Decode(dest, src []byte) ([]byte, error) {

// DecodeString convert a string from a specific charset to utf-8 charset.
func (e *Encoding) DecodeString(src string) (string, error) {
if e.name == encodings[CharsetUTF8].name {
Copy link
Member

Choose a reason for hiding this comment

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

WIll an extra check cause a performance regression?

Copy link
Member

Choose a reason for hiding this comment

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

DecodeString will called by the lexer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it will. Maybe #30288 is a better way to do this. I will disscuss with @tangenta later.

Copy link
Member

Choose a reason for hiding this comment

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

@tangenta @Defined2014 Do you still want to go on with this issue, or leave it with #30288?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I will close it.

@Defined2014
Copy link
Contributor Author

It's be covered by #30288.

@Defined2014 Defined2014 deleted the issue-29685 branch December 10, 2021 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
5 participants