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

executor: add placeholder count check in prepare stage (#7162) #7250

Merged
merged 5 commits into from Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions executor/executor.go
Expand Up @@ -76,6 +76,7 @@ var (
ErrWrongValueCountOnRow = terror.ClassExecutor.New(codeWrongValueCountOnRow, "Column count doesn't match value count at row %d")
ErrPasswordFormat = terror.ClassExecutor.New(codePasswordFormat, "The password hash doesn't have the expected format. Check if the correct password algorithm is being used with the PASSWORD() function.")
ErrCantChangeTxCharacteristics = terror.ClassExecutor.New(codeErrCantChangeTxCharacteristics, "Transaction characteristics can't be changed while a transaction is in progress")
ErrPsManyParam = terror.ClassExecutor.New(mysql.ErrPsManyParam, mysql.MySQLErrName[mysql.ErrPsManyParam])
)

// Error codes.
Expand All @@ -91,6 +92,7 @@ const (
codeWrongValueCountOnRow terror.ErrCode = 1136 // MySQL error code
codePasswordFormat terror.ErrCode = 1827 // MySQL error code
codeErrCantChangeTxCharacteristics terror.ErrCode = 1568
codeErrPsManyParam terror.ErrCode = 1390
)

type baseExecutor struct {
Expand Down Expand Up @@ -611,6 +613,7 @@ func init() {
codeWrongValueCountOnRow: mysql.ErrWrongValueCountOnRow,
codePasswordFormat: mysql.ErrPasswordFormat,
codeErrCantChangeTxCharacteristics: mysql.ErrCantChangeTxCharacteristics,
codeErrPsManyParam: mysql.ErrPsManyParam,
}
terror.ErrClassToMySQLCodes[terror.ClassExecutor] = tableMySQLErrCodes
}
Expand Down
7 changes: 7 additions & 0 deletions executor/prepared.go
Expand Up @@ -126,6 +126,13 @@ func (e *PrepareExec) Next(ctx context.Context, chk *chunk.Chunk) error {
}
var extractor paramMarkerExtractor
stmt.Accept(&extractor)

// Prepare parameters should NOT over 2 bytes(MaxUint16)
// https://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html#packet-COM_STMT_PREPARE_OK.
if len(extractor.markers) > math.MaxUint16 {
return ErrPsManyParam
}

err = plan.Preprocess(e.ctx, stmt, e.is, true)
if err != nil {
return errors.Trace(err)
Expand Down
28 changes: 28 additions & 0 deletions executor/prepared_test.go
Expand Up @@ -14,6 +14,9 @@
package executor_test

import (
"math"
"strings"

"github.com/juju/errors"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/executor"
Expand Down Expand Up @@ -267,3 +270,28 @@ func (s *testSuite) TestPreparedNameResolver(c *C) {
_, err = tk.Exec("prepare stmt from '(select * FROM t) union all (select * FROM t) order by a limit ?'")
c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'order clause'")
}

func (s *testSuite) TestPrepareMaxParamCountCheck(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (v int)")
normalSQL, normalParams := generateBatchSQL(math.MaxUint16)
_, err := tk.Exec(normalSQL, normalParams...)
c.Assert(err, IsNil)

bigSQL, bigParams := generateBatchSQL(math.MaxUint16 + 2)
_, err = tk.Exec(bigSQL, bigParams...)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[executor:1390]Prepared statement contains too many placeholders")
}

func generateBatchSQL(paramCount int) (sql string, paramSlice []interface{}) {
params := make([]interface{}, 0, paramCount)
placeholders := make([]string, 0, paramCount)
for i := 0; i < paramCount; i++ {
params = append(params, i)
placeholders = append(placeholders, "(?)")
}
return "insert into t values " + strings.Join(placeholders, ","), params
}