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

server: fix binary numeric type overflow #6922

Merged
merged 8 commits into from
Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions server/conn_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy
}

if isUnsigned {
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I think binary int does not have an unsigned flag.

args[i] = uint64(paramValues[pos])
args[i] = uint8(paramValues[pos])
Copy link
Member

Choose a reason for hiding this comment

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

Why use uint8?

} else {
args[i] = int64(paramValues[pos])
args[i] = int8(paramValues[pos])
}

pos++
Expand All @@ -288,9 +288,9 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy
}
valU16 := binary.LittleEndian.Uint16(paramValues[pos : pos+2])
if isUnsigned {
args[i] = uint64(valU16)
args[i] = valU16
} else {
args[i] = int64(valU16)
args[i] = int16(valU16)
}
pos += 2
continue
Expand All @@ -302,9 +302,9 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy
}
valU32 := binary.LittleEndian.Uint32(paramValues[pos : pos+4])
if isUnsigned {
args[i] = uint64(valU32)
args[i] = valU32
} else {
args[i] = int64(valU32)
args[i] = int32(valU32)
}
pos += 4
continue
Expand All @@ -329,7 +329,7 @@ func parseStmtArgs(args []interface{}, boundParams [][]byte, nullBitmap, paramTy
return
}

args[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(paramValues[pos : pos+4])))
args[i] = math.Float32frombits(binary.LittleEndian.Uint32(paramValues[pos : pos+4]))
Copy link
Member Author

Choose a reason for hiding this comment

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

Don't know why there is a cast here, either.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to add a test case for this type?

Copy link
Member Author

Choose a reason for hiding this comment

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

No need to do it.

  1. There's no signed or unsigned float.
  2. jdbc test cases in tidb-test will cover this. The unit test is not suitable for this case.

pos += 4
continue

Expand Down
34 changes: 34 additions & 0 deletions server/conn_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ func (ts ConnTestSuite) TestParseStmtArgs(c *C) {
err error
expect interface{}
}{
// Tests for int overflow
{
args{
make([]interface{}, 1),
[][]byte{nil},
[]byte{0x0},
[]byte{1, 0},
[]byte{0xff},
},
nil,
int8(-1),
},
{
args{
make([]interface{}, 1),
[][]byte{nil},
[]byte{0x0},
[]byte{2, 0},
[]byte{0xff, 0xff},
},
nil,
int16(-1),
},
{
args{
make([]interface{}, 1),
[][]byte{nil},
[]byte{0x0},
[]byte{3, 0},
[]byte{0xff, 0xff, 0xff, 0xff},
},
nil,
int32(-1),
},
// Tests for date/datetime/timestamp
{
args{
Expand Down