types: check overflow for bit type
This commit is contained in:
@ -370,7 +370,26 @@ func Convert(val interface{}, target *FieldType) (v interface{}, err error) { //
|
||||
}
|
||||
return convertToInt(val, target)
|
||||
case mysql.TypeBit:
|
||||
return convertToUint(val, target)
|
||||
x, err := convertToUint(val, target)
|
||||
if err != nil {
|
||||
return x, errors.Trace(err)
|
||||
}
|
||||
|
||||
// check bit boundary, if bit has n width, the boundary is
|
||||
// in [0, (1 << n) - 1]
|
||||
width := target.Flen
|
||||
if width == 0 {
|
||||
width = mysql.MinBitWidth
|
||||
} else if width == mysql.UnspecifiedBitWidth {
|
||||
width = mysql.MaxBitWidth
|
||||
}
|
||||
|
||||
maxValue := uint64(1)<<uint64(width) - 1
|
||||
|
||||
if x > maxValue {
|
||||
return maxValue, overflow(val, tp)
|
||||
}
|
||||
return x, nil
|
||||
case mysql.TypeDecimal, mysql.TypeNewDecimal:
|
||||
x, err := ToDecimal(val)
|
||||
if err != nil {
|
||||
|
||||
@ -169,6 +169,7 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {
|
||||
|
||||
// For TypeBit
|
||||
ft = NewFieldType(mysql.TypeBit)
|
||||
ft.Flen = 8
|
||||
v, err = Convert("100", ft)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(v, Equals, uint64(100))
|
||||
@ -181,6 +182,18 @@ func (s *testTypeConvertSuite) TestConvertType(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(v, Equals, uint64(100))
|
||||
|
||||
ft.Flen = 1
|
||||
v, err = Convert(1, ft)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(v, Equals, uint64(1))
|
||||
|
||||
_, err = Convert(2, ft)
|
||||
c.Assert(err, NotNil)
|
||||
|
||||
ft.Flen = 0
|
||||
_, err = Convert(2, ft)
|
||||
c.Assert(err, NotNil)
|
||||
|
||||
// For TypeNewDecimal
|
||||
ft = NewFieldType(mysql.TypeNewDecimal)
|
||||
ft.Decimal = 5
|
||||
|
||||
Reference in New Issue
Block a user