Skip to content

Commit

Permalink
sql/sem/tree: use arith package for sub overflow checking
Browse files Browse the repository at this point in the history
This puts all the overflow logic into a single package.
  • Loading branch information
maddyblue committed Apr 19, 2019
1 parent badf854 commit 89b76af
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions pkg/sql/sem/tree/eval.go
Expand Up @@ -698,13 +698,11 @@ var BinOps = map[BinaryOperator]binOpOverload{
ReturnType: types.Int,
Fn: func(_ *EvalContext, left Datum, right Datum) (Datum, error) {
a, b := MustBeDInt(left), MustBeDInt(right)
if b < 0 && a > math.MaxInt64+b {
return nil, errIntOutOfRange
}
if b > 0 && a < math.MinInt64+b {
r, ok := arith.SubWithOverflow(int64(a), int64(b))
if !ok {
return nil, errIntOutOfRange
}
return NewDInt(a - b), nil
return NewDInt(DInt(r)), nil
},
},
&BinOp{
Expand Down

0 comments on commit 89b76af

Please sign in to comment.