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 uint64 overflow bug in ConvertJSONToFloat #11355

Merged
merged 5 commits into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions types/convert.go
Expand Up @@ -553,8 +553,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(), IntergerUnsignedUpperBound(mysql.TypeLonglong), mysql.TypeLonglong)
Copy link
Contributor

@winkyao winkyao Jul 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider ShouldClipToZero?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to consider this.
we extract a uint from the json, it must not < 0.

// GetInt64 gets the int64 value.
func (bj BinaryJSON) GetInt64() int64 {
	return int64(endian.Uint64(bj.Value))
}

// GetUint64 gets the uint64 value.
func (bj BinaryJSON) GetUint64() uint64 {
	return endian.Uint64(bj.Value)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need or needn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needn't

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 @@ -828,24 +828,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