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

types: fix undefined behavior in numeric types cast (cast 1<<64 to uint64) #11968

Merged
merged 9 commits into from
Oct 29, 2019
4 changes: 4 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ func (b *builtinCastRealAsIntSig) evalInt(row chunk.Row) (res int64, isNull bool
uintVal, err = types.ConvertFloatToUint(sc, val, types.IntergerUnsignedUpperBound(mysql.TypeLonglong), mysql.TypeLonglong)
res = int64(uintVal)
}
err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err)
if err != nil {
return res, false, err
}
return res, isNull, err
}

Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (b *builtinCastRealAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk.C
uintVal, err = types.ConvertFloatToUint(sc, f64s[i], types.IntergerUnsignedUpperBound(mysql.TypeLonglong), mysql.TypeLonglong)
i64s[i] = int64(uintVal)
}

err = b.ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,13 @@ func (s *testIntegrationSuite2) TestBuiltin(c *C) {
c.Assert(last, Equals, "bigint")
tk.MustExec(`drop table tb5;`)

tk.MustExec(`create table tb5(a double(64));`)
tk.MustExec(`insert into test.tb5 (a) values (18446744073709551616);`)
tk.MustExec(`insert into test.tb5 (a) values (184467440737095516160);`)
result = tk.MustQuery(`select cast(a as unsigned) from test.tb5;`)
result.Check(testkit.Rows("9223372036854775807", "9223372036854775807"))
tk.MustExec(`drop table tb5`)

// test builtinCastIntAsDecimalSig
tk.MustExec(`create table tb5(a bigint(64) unsigned, b decimal(64, 10));`)
tk.MustExec(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`)
Expand Down
8 changes: 6 additions & 2 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ func ConvertFloatToUint(sc *stmtctx.StatementContext, fval float64, upperBound u
return uint64(int64(val)), overflow(val, tp)
}

if val > float64(upperBound) {
return upperBound, overflow(val, tp)
ubf := float64(upperBound)
if val == ubf {
return uint64(math.MaxInt64), nil
}
if val > ubf {
return uint64(math.MaxInt64), overflow(val, tp)
}
return uint64(val), nil
}
Expand Down
2 changes: 1 addition & 1 deletion types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func (s *testTypeConvertSuite) TestConvert(c *C) {
unsignedAccept(c, mysql.TypeLonglong, -1, "18446744073709551615")
unsignedAccept(c, mysql.TypeLonglong, 0, "0")
unsignedAccept(c, mysql.TypeLonglong, uint64(math.MaxUint64), strvalue(uint64(math.MaxUint64)))
unsignedDeny(c, mysql.TypeLonglong, math.MaxUint64*1.1, strvalue(uint64(math.MaxUint64)))
unsignedDeny(c, mysql.TypeLonglong, math.MaxUint64*1.1, strvalue(uint64(math.MaxInt64)))

// integer from string
signedAccept(c, mysql.TypeLong, " 234 ", "234")
Expand Down