Skip to content

Commit

Permalink
expression: handle corrupted length in uncompress builtin function
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyi-yan committed Dec 5, 2018
1 parent ac3556b commit d43bb87
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions expression/builtin_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,16 @@ func (b *builtinUncompressSig) evalString(row chunk.Row) (string, bool, error) {
sc.AppendWarning(errZlibZData)
return "", true, nil
}
length := binary.LittleEndian.Uint32([]byte(payload[0:4]))
bytes, err := inflate([]byte(payload[4:]))
if err != nil {
sc.AppendWarning(errZlibZData)
return "", true, nil
}
if length < uint32(len(bytes)) {
sc.AppendWarning(errZlibZBuf)
return "", true, nil
}
return string(bytes), false, nil
}

Expand Down
1 change: 1 addition & 0 deletions expression/builtin_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ func (s *testEvaluatorSuite) TestUncompress(c *C) {
}{
{decodeHex("0B000000789CCB48CDC9C95728CF2FCA4901001A0B045D"), "hello world"}, // zlib result from MySQL
{decodeHex("0B000000789CCA48CDC9C95728CF2FCA4901040000FFFF1A0B045D"), "hello world"}, // zlib result from TiDB
{decodeHex("02000000789CCB48CDC9C95728CF2FCA4901001A0B045D"), nil}, // wrong length in the first four bytes
{decodeHex(""), ""},
{"1", nil},
{"1234", nil},
Expand Down
4 changes: 3 additions & 1 deletion expression/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var (

// All the un-exported errors are defined here:
errFunctionNotExists = terror.ClassExpression.New(mysql.ErrSpDoesNotExist, mysql.MySQLErrName[mysql.ErrSpDoesNotExist])
errZlibZData = terror.ClassTypes.New(mysql.ErrZlibZData, mysql.MySQLErrName[mysql.ErrZlibZData])
errZlibZData = terror.ClassExpression.New(mysql.ErrZlibZData, mysql.MySQLErrName[mysql.ErrZlibZData])
errZlibZBuf = terror.ClassExpression.New(mysql.ErrZlibZBuf, mysql.MySQLErrName[mysql.ErrZlibZBuf])
errIncorrectArgs = terror.ClassExpression.New(mysql.ErrWrongArguments, mysql.MySQLErrName[mysql.ErrWrongArguments])
errUnknownCharacterSet = terror.ClassExpression.New(mysql.ErrUnknownCharacterSet, mysql.MySQLErrName[mysql.ErrUnknownCharacterSet])
errDefaultValue = terror.ClassExpression.New(mysql.ErrInvalidDefault, "invalid default value")
Expand All @@ -48,6 +49,7 @@ func init() {
mysql.ErrDivisionByZero: mysql.ErrDivisionByZero,
mysql.ErrSpDoesNotExist: mysql.ErrSpDoesNotExist,
mysql.ErrZlibZData: mysql.ErrZlibZData,
mysql.ErrZlibZBuf: mysql.ErrZlibZBuf,
mysql.ErrWrongArguments: mysql.ErrWrongArguments,
mysql.ErrUnknownCharacterSet: mysql.ErrUnknownCharacterSet,
mysql.ErrInvalidDefault: mysql.ErrInvalidDefault,
Expand Down

0 comments on commit d43bb87

Please sign in to comment.