Skip to content

Commit

Permalink
server: fix connection reset caused by alias length overflow (#14870)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song committed Feb 25, 2020
1 parent 8431c10 commit 18ce601
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions server/column.go
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/pingcap/parser/mysql"
)

const maxColumnNameSize = 256

// ColumnInfo contains information of a column
type ColumnInfo struct {
Schema string
Expand All @@ -35,12 +37,19 @@ type ColumnInfo struct {

// Dump dumps ColumnInfo to bytes.
func (column *ColumnInfo) Dump(buffer []byte) []byte {
nameDump, orgnameDump := []byte(column.Name), []byte(column.OrgName)
if len(nameDump) > maxColumnNameSize {
nameDump = nameDump[0:maxColumnNameSize]
}
if len(orgnameDump) > maxColumnNameSize {
orgnameDump = orgnameDump[0:maxColumnNameSize]
}
buffer = dumpLengthEncodedString(buffer, []byte("def"))
buffer = dumpLengthEncodedString(buffer, []byte(column.Schema))
buffer = dumpLengthEncodedString(buffer, []byte(column.Table))
buffer = dumpLengthEncodedString(buffer, []byte(column.OrgTable))
buffer = dumpLengthEncodedString(buffer, []byte(column.Name))
buffer = dumpLengthEncodedString(buffer, []byte(column.OrgName))
buffer = dumpLengthEncodedString(buffer, nameDump)
buffer = dumpLengthEncodedString(buffer, orgnameDump)

buffer = append(buffer, 0x0c)

Expand Down
24 changes: 24 additions & 0 deletions server/column_test.go
Expand Up @@ -50,3 +50,27 @@ func (s ColumnTestSuite) TestDumpColumn(c *C) {
c.Assert(dumpType(mysql.TypeEnum), Equals, mysql.TypeString)
c.Assert(dumpType(mysql.TypeBit), Equals, mysql.TypeBit)
}

func (s ColumnTestSuite) TestColumnNameLimit(c *C) {
aLongName := make([]byte, 0, 300)
for i := 0; i < 300; i++ {
aLongName = append(aLongName, 'a')
}
info := ColumnInfo{
Schema: "testSchema",
Table: "testTable",
OrgTable: "testOrgTable",
Name: string(aLongName),
OrgName: "testOrgName",
ColumnLength: 1,
Charset: 106,
Flag: 0,
Decimal: 1,
Type: 14,
DefaultValueLength: 2,
DefaultValue: []byte{5, 2},
}
r := info.Dump(nil)
exp := []byte{0x3, 0x64, 0x65, 0x66, 0xa, 0x74, 0x65, 0x73, 0x74, 0x53, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x9, 0x74, 0x65, 0x73, 0x74, 0x54, 0x61, 0x62, 0x6c, 0x65, 0xc, 0x74, 0x65, 0x73, 0x74, 0x4f, 0x72, 0x67, 0x54, 0x61, 0x62, 0x6c, 0x65, 0xfc, 0x0, 0x1, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0xb, 0x74, 0x65, 0x73, 0x74, 0x4f, 0x72, 0x67, 0x4e, 0x61, 0x6d, 0x65, 0xc, 0x6a, 0x0, 0x1, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x1, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, 0x2}
c.Assert(r, DeepEquals, exp)
}

0 comments on commit 18ce601

Please sign in to comment.