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

arithmetic overflow #143

Merged
merged 5 commits into from
Sep 15, 2015
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
23 changes: 7 additions & 16 deletions expression/expressions/binop.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ func (o *BinaryOperation) evalComparisonOp(ctx context.Context, args map[interfa
}

func (o *BinaryOperation) evalPlus(a interface{}, b interface{}) (interface{}, error) {
// TODO: check overflow
switch x := a.(type) {
case int64:
switch y := b.(type) {
Expand Down Expand Up @@ -443,7 +442,6 @@ func (o *BinaryOperation) evalPlus(a interface{}, b interface{}) (interface{}, e
}

func (o *BinaryOperation) evalMinus(a interface{}, b interface{}) (interface{}, error) {
// TODO: check overflow
switch x := a.(type) {
case int64:
switch y := b.(type) {
Expand Down Expand Up @@ -475,7 +473,6 @@ func (o *BinaryOperation) evalMinus(a interface{}, b interface{}) (interface{},
}

func (o *BinaryOperation) evalMul(a interface{}, b interface{}) (interface{}, error) {
// TODO: check overflow
switch x := a.(type) {
case int64:
switch y := b.(type) {
Expand Down Expand Up @@ -554,24 +551,20 @@ func (o *BinaryOperation) evalIntDiv(a interface{}, b interface{}) (interface{},
if y == 0 {
return nil, nil
}
return x / y, nil
return types.DivInt64(x, y)
case uint64:
if y == 0 {
return nil, nil
}
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return uint64(x) / y, nil
return types.DivIntWithUint(x, y)
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
}
// For MySQL, if any is unsigned, return unsigned
// TODO: check overflow
return x / uint64(y), nil
return types.DivUintWithInt(x, y)
case uint64:
if y == 0 {
return nil, nil
Expand Down Expand Up @@ -611,22 +604,20 @@ func (o *BinaryOperation) evalMod(a interface{}, b interface{}) (interface{}, er
if y == 0 {
return nil, nil
} else if x < 0 {
// TODO: check overflow
// first is int64, return int64.
return -int64(uint64(-x) % y), nil
}
// TODO: check overflow
return uint64(x) % y, nil
return int64(uint64(x) % y), nil
}
case uint64:
switch y := b.(type) {
case int64:
if y == 0 {
return nil, nil
} else if y < 0 {
// TODO: check overflow
return -int64(x % uint64(-y)), nil
// first is uint64, return uint64.
return uint64(x % uint64(-y)), nil
}
// TODO: check overflow
return x % uint64(y), nil
case uint64:
if y == 0 {
Expand Down
38 changes: 38 additions & 0 deletions util/types/overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,41 @@ func MulInteger(a uint64, b int64) (uint64, error) {

return MulUint64(a, uint64(b))
}

// DivInt64 divides int64 a with b, returns int64 if no overflow error.
// It just checks overflow, if b is zero, a "divide by zero" panic throws.
func DivInt64(a int64, b int64) (int64, error) {
if a == math.MinInt64 && b == -1 {
return 0, errors.Trace(ErrArithOverflow)
}

return a / b, nil
}

// DivUintWithInt divides uint64 a with int64 b, returns uint64 if no overflow error.
// It just checks overflow, if b is zero, a "divide by zero" panic throws.
func DivUintWithInt(a uint64, b int64) (uint64, error) {
if b < 0 {
if a != 0 && uint64(-b) <= a {
return 0, errors.Trace(ErrArithOverflow)
}

return 0, nil
}

return a / uint64(b), nil
}

// DivIntWithUint divides int64 a with uint64 b, returns uint64 if no overflow error.
// It just checks overflow, if b is zero, a "divide by zero" panic throws.
func DivIntWithUint(a int64, b uint64) (uint64, error) {
if a < 0 {
if uint64(-a) >= b {
return 0, errors.Trace(ErrArithOverflow)
}

return 0, nil
}

return uint64(a) / b, nil
}
68 changes: 68 additions & 0 deletions util/types/overflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,71 @@ func (s *testOverflowSuite) TestMul(c *C) {
}
}
}

func (s *testOverflowSuite) TestDiv(c *C) {
tblInt64 := []struct {
lsh int64
rsh int64
ret int64
overflow bool
}{
{math.MaxInt64, 1, math.MaxInt64, false},
{math.MinInt64, 1, math.MinInt64, false},
{math.MinInt64, -1, 0, true},
{math.MaxInt64, -1, -math.MaxInt64, false},
{1, -1, -1, false},
{-1, 1, -1, false},
{-1, 2, 0, false},
{math.MinInt64, 2, math.MinInt64 / 2, false},
}

for _, t := range tblInt64 {
ret, err := DivInt64(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
} else {
c.Assert(ret, Equals, t.ret)
}
}

tblInt := []struct {
lsh uint64
rsh int64
ret uint64
overflow bool
}{
{0, -1, 0, false},
{1, -1, 0, true},
{math.MaxInt64, math.MinInt64, 0, false},
{math.MaxInt64, -1, 0, true},
}

for _, t := range tblInt {
ret, err := DivUintWithInt(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
} else {
c.Assert(ret, Equals, t.ret)
}
}

tblInt2 := []struct {
lsh int64
rsh uint64
ret uint64
overflow bool
}{
{math.MinInt64, math.MaxInt64, 0, true},
{0, 1, 0, false},
{-1, math.MaxInt64, 0, false},
}

for _, t := range tblInt2 {
ret, err := DivIntWithUint(t.lsh, t.rsh)
if t.overflow {
c.Assert(err, NotNil)
} else {
c.Assert(ret, Equals, t.ret)
}
}
}