From 18ce601629fd233b1a7d50c7aa8dddfaca44cd63 Mon Sep 17 00:00:00 2001 From: Lingyu Song Date: Tue, 25 Feb 2020 21:40:07 +0800 Subject: [PATCH] server: fix connection reset caused by alias length overflow (#14870) --- server/column.go | 13 +++++++++++-- server/column_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/server/column.go b/server/column.go index 195061c3be875..677dc702eec13 100644 --- a/server/column.go +++ b/server/column.go @@ -17,6 +17,8 @@ import ( "github.com/pingcap/parser/mysql" ) +const maxColumnNameSize = 256 + // ColumnInfo contains information of a column type ColumnInfo struct { Schema string @@ -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) diff --git a/server/column_test.go b/server/column_test.go index b5baca7e1586e..c87f972811aed 100644 --- a/server/column_test.go +++ b/server/column_test.go @@ -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) +}