Skip to content

Commit

Permalink
types: fix uint64 overflow bug in ConvertJSONToFloat (#11355) (#11440)
Browse files Browse the repository at this point in the history
All tests passed, auto merged by Bot
  • Loading branch information
H-ZeX authored and sre-bot committed Jul 26, 2019
1 parent 4ee1c52 commit 711d142
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
6 changes: 5 additions & 1 deletion expression/integration_test.go
Expand Up @@ -2035,10 +2035,14 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) {
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery(`select cast(cast('2017-01-01 01:01:11.12' as date) as datetime(2));`)
result.Check(testkit.Rows("2017-01-01 00:00:00.00"))

result = tk.MustQuery(`select cast(20170118.999 as datetime);`)
result.Check(testkit.Rows("2017-01-18 00:00:00"))

tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`)
tk.MustExec(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`)
tk.MustExec(`insert into tb5 (a, b) select * from (select cast(a as json) as a1, b from tb5) as t where t.a1 = t.b;`)
tk.MustExec(`drop table tb5;`)

// Test corner cases of cast string as datetime
result = tk.MustQuery(`select cast("170102034" as datetime);`)
result.Check(testkit.Rows("2017-01-02 03:04:00"))
Expand Down
3 changes: 1 addition & 2 deletions types/convert.go
Expand Up @@ -523,8 +523,7 @@ func ConvertJSONToFloat(sc *stmtctx.StatementContext, j json.BinaryJSON) (float6
case json.TypeCodeInt64:
return float64(j.GetInt64()), nil
case json.TypeCodeUint64:
u, err := ConvertIntToUint(sc, j.GetInt64(), UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeLonglong)
return float64(u), errors.Trace(err)
return float64(j.GetUint64()), nil
case json.TypeCodeFloat64:
return j.GetFloat64(), nil
case json.TypeCodeString:
Expand Down
30 changes: 16 additions & 14 deletions types/convert_test.go
Expand Up @@ -811,24 +811,26 @@ func (s *testTypeConvertSuite) TestConvertJSONToInt(c *C) {

func (s *testTypeConvertSuite) TestConvertJSONToFloat(c *C) {
var tests = []struct {
In string
In interface{}
Out float64
ty json.TypeCode
}{
{`{}`, 0},
{`[]`, 0},
{`3`, 3},
{`-3`, -3},
{`4.5`, 4.5},
{`true`, 1},
{`false`, 0},
{`null`, 0},
{`"hello"`, 0},
{`"123.456hello"`, 123.456},
{`"1234"`, 1234},
{make(map[string]interface{}, 0), 0, json.TypeCodeObject},
{make([]interface{}, 0), 0, json.TypeCodeArray},
{int64(3), 3, json.TypeCodeInt64},
{int64(-3), -3, json.TypeCodeInt64},
{uint64(1 << 63), 1 << 63, json.TypeCodeUint64},
{float64(4.5), 4.5, json.TypeCodeFloat64},
{true, 1, json.TypeCodeLiteral},
{false, 0, json.TypeCodeLiteral},
{nil, 0, json.TypeCodeLiteral},
{"hello", 0, json.TypeCodeString},
{"123.456hello", 123.456, json.TypeCodeString},
{"1234", 1234, json.TypeCodeString},
}
for _, tt := range tests {
j, err := json.ParseBinaryFromString(tt.In)
c.Assert(err, IsNil)
j := json.CreateBinary(tt.In)
c.Assert(j.TypeCode, Equals, tt.ty)
casted, _ := ConvertJSONToFloat(new(stmtctx.StatementContext), j)
c.Assert(casted, Equals, tt.Out)
}
Expand Down
1 change: 1 addition & 0 deletions types/datum_test.go
Expand Up @@ -218,6 +218,7 @@ func (ts *testDatumSuite) TestToJSON(c *C) {
{NewStringDatum("[1, 2, 3]"), `[1, 2, 3]`, true},
{NewStringDatum("{}"), `{}`, true},
{mustParseTimeIntoDatum("2011-11-10 11:11:11.111111", mysql.TypeTimestamp, 6), `"2011-11-10 11:11:11.111111"`, true},
{NewStringDatum(`{"a": "9223372036854775809"}`), `{"a": "9223372036854775809"}`, true},

// can not parse JSON from this string, so error occurs.
{NewStringDatum("hello, 世界"), "", false},
Expand Down

0 comments on commit 711d142

Please sign in to comment.