Have bounds checks use interval.IntRange methods

This commit is contained in:
Nigel Tao committed 2018-12-26 15:13:56 +11:00
1 parent 20e82ba97b
commit 231389ee26
1 file changed
+28 -58
+28 -58
View File
@@ -1257,15 +1257,11 @@ func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (bounds, error) {
} }
func (q *checker) bcheckExprXBinaryPlus(lhs *a.Expr, lb bounds, rhs *a.Expr, rb bounds) (bounds, error) { func (q *checker) bcheckExprXBinaryPlus(lhs *a.Expr, lb bounds, rhs *a.Expr, rb bounds) (bounds, error) {
return bounds{ return lb.Add(rb), nil
big.NewInt(0).Add(lb[0], rb[0]),
big.NewInt(0).Add(lb[1], rb[1]),
}, nil
} }
func (q *checker) bcheckExprXBinaryMinus(lhs *a.Expr, lb bounds, rhs *a.Expr, rb bounds) (bounds, error) { func (q *checker) bcheckExprXBinaryMinus(lhs *a.Expr, lb bounds, rhs *a.Expr, rb bounds) (bounds, error) {
nMin := big.NewInt(0).Sub(lb[0], rb[1]) nb := lb.Sub(rb)
nMax := big.NewInt(0).Sub(lb[1], rb[0])
for _, x := range q.facts { for _, x := range q.facts {
xOp, xLHS, xRHS := parseBinaryOp(x) xOp, xLHS, xRHS := parseBinaryOp(x)
if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) { if !lhs.Eq(xLHS) || !rhs.Eq(xRHS) {
@@ -1273,16 +1269,16 @@ func (q *checker) bcheckExprXBinaryMinus(lhs *a.Expr, lb bounds, rhs *a.Expr, rb
} }
switch xOp { switch xOp {
case t.IDXBinaryLessThan: case t.IDXBinaryLessThan:
nMax = min(nMax, minusOne) nb[1] = min(nb[1], minusOne)
case t.IDXBinaryLessEq: case t.IDXBinaryLessEq:
nMax = min(nMax, zero) nb[1] = min(nb[1], zero)
case t.IDXBinaryGreaterEq: case t.IDXBinaryGreaterEq:
nMin = max(nMin, zero) nb[0] = max(nb[0], zero)
case t.IDXBinaryGreaterThan: case t.IDXBinaryGreaterThan:
nMin = max(nMin, one) nb[0] = max(nb[0], one)
} }
} }
return bounds{nMin, nMax}, nil return nb, nil
} }
func (q *checker) bcheckExprBinaryOp(op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) (bounds, error) { func (q *checker) bcheckExprBinaryOp(op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) (bounds, error) {
@@ -1307,18 +1303,7 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lb bounds, rhs *a.Ex
return q.bcheckExprXBinaryMinus(lhs, lb, rhs, rb) return q.bcheckExprXBinaryMinus(lhs, lb, rhs, rb)
case t.IDXBinaryStar: case t.IDXBinaryStar:
// TODO: handle multiplication by negative numbers. Note that this return lb.Mul(rb), nil
// might reverse the inequality: if 0 < a < b but c < 0 then a*c > b*c.
if lb[0].Sign() < 0 {
return bounds{}, fmt.Errorf("check: multiply op argument %q is possibly negative", lhs.Str(q.tm))
}
if rb[0].Sign() < 0 {
return bounds{}, fmt.Errorf("check: multiply op argument %q is possibly negative", rhs.Str(q.tm))
}
return bounds{
big.NewInt(0).Mul(lb[0], rb[0]),
big.NewInt(0).Mul(lb[1], rb[1]),
}, nil
case t.IDXBinarySlash, t.IDXBinaryPercent: case t.IDXBinarySlash, t.IDXBinaryPercent:
// Prohibit division by zero. // Prohibit division by zero.
@@ -1329,10 +1314,8 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lb bounds, rhs *a.Ex
return bounds{}, fmt.Errorf("check: divide/modulus op argument %q is possibly non-positive", rhs.Str(q.tm)) return bounds{}, fmt.Errorf("check: divide/modulus op argument %q is possibly non-positive", rhs.Str(q.tm))
} }
if op == t.IDXBinarySlash { if op == t.IDXBinarySlash {
return bounds{ nb, _ := lb.Quo(rb)
big.NewInt(0).Mul(lb[0], rb[1]), return nb, nil
big.NewInt(0).Mul(lb[1], rb[0]),
}, nil
} }
return bounds{ return bounds{
zero, zero,
@@ -1349,15 +1332,14 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lb bounds, rhs *a.Ex
if rb[1].Cmp(ffff) > 0 { if rb[1].Cmp(ffff) > 0 {
return bounds{}, fmt.Errorf("check: shift %q out of range", rhs.Str(q.tm)) return bounds{}, fmt.Errorf("check: shift %q out of range", rhs.Str(q.tm))
} }
nMin := big.NewInt(0).Lsh(lb[0], uint(rb[0].Uint64())) nb, _ := lb.Lsh(rb)
nMax := big.NewInt(0).Lsh(lb[1], uint(rb[1].Uint64()))
if op == t.IDXBinaryTildeModShiftL { if op == t.IDXBinaryTildeModShiftL {
if qid := lhs.MType().QID(); qid[0] == t.IDBase { if qid := lhs.MType().QID(); qid[0] == t.IDBase {
b := numTypeBounds[qid[1]] b := numTypeBounds[qid[1]]
nMax = min(nMax, b[1]) nb[1] = min(nb[1], b[1])
} }
} }
return bounds{nMin, nMax}, nil return nb, nil
case t.IDXBinaryShiftR: case t.IDXBinaryShiftR:
if lb[0].Sign() < 0 { if lb[0].Sign() < 0 {
@@ -1366,17 +1348,8 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lb bounds, rhs *a.Ex
if rb[0].Sign() < 0 { if rb[0].Sign() < 0 {
return bounds{}, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm)) return bounds{}, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm))
} }
if rb[0].Cmp(maxIntBits) >= 0 { nb, _ := lb.Rsh(rb)
return bounds{zero, zero}, nil return nb, nil
}
nMax := big.NewInt(0).Rsh(lb[1], uint(rb[0].Uint64()))
if rb[1].Cmp(maxIntBits) >= 0 {
return bounds{zero, nMax}, nil
}
return bounds{
big.NewInt(0).Rsh(lb[0], uint(rb[1].Uint64())),
nMax,
}, nil
case t.IDXBinaryAmp, t.IDXBinaryPipe, t.IDXBinaryHat: case t.IDXBinaryAmp, t.IDXBinaryPipe, t.IDXBinaryHat:
// TODO: should type-checking ensure that bitwise ops only apply to // TODO: should type-checking ensure that bitwise ops only apply to
@@ -1384,28 +1357,25 @@ func (q *checker) bcheckExprBinaryOp1(op t.ID, lhs *a.Expr, lb bounds, rhs *a.Ex
if lb[0].Sign() < 0 { if lb[0].Sign() < 0 {
return bounds{}, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.Str(q.tm)) return bounds{}, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.Str(q.tm))
} }
if lb[1].Cmp(numTypeBounds[t.IDU64][1]) > 0 {
return bounds{}, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.Str(q.tm))
}
if rb[0].Sign() < 0 { if rb[0].Sign() < 0 {
return bounds{}, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.Str(q.tm)) return bounds{}, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.Str(q.tm))
} }
if rb[1].Cmp(numTypeBounds[t.IDU64][1]) > 0 {
return bounds{}, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.Str(q.tm))
}
z := (*big.Int)(nil)
switch op { switch op {
case t.IDXBinaryAmp: case t.IDXBinaryAmp:
z = min(lb[1], rb[1]) nb, _ := lb.And(rb)
case t.IDXBinaryPipe, t.IDXBinaryHat: return nb, nil
z = max(lb[1], rb[1]) case t.IDXBinaryPipe:
nb, _ := lb.Or(rb)
return nb, nil
case t.IDXBinaryHat:
z := max(lb[1], rb[1])
// Return [0, z rounded up to the next power-of-2-minus-1]. This is
// conservative, but works fine in practice.
return bounds{
zero,
bitMask(z.BitLen()),
}, nil
} }
// Return [0, z rounded up to the next power-of-2-minus-1]. This is
// conservative, but works fine in practice.
return bounds{
zero,
bitMask(z.BitLen()),
}, nil
case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus: case t.IDXBinaryTildeModPlus, t.IDXBinaryTildeModMinus:
typ := lhs.MType() typ := lhs.MType()