Bounds check intermediate expressions.

When checking "(a + b) * 0", "a + b" might still overflow.
This commit is contained in:
Nigel Tao committed 2017-06-10 11:41:20 +10:00
1 parent ebf3713555
commit 793eb043a4
4 files changed
+53 -35

No files matched your search

+10
View File
@@ -481,6 +481,16 @@ func (n *TypeExpr) IsRefined() bool {
return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil) return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil)
} }
func (n *TypeExpr) Unrefined() *TypeExpr {
if !n.IsRefined() {
return n
}
o := *n
o.lhs = nil
o.mhs = nil
return &o
}
func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inner *TypeExpr) *TypeExpr { func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inner *TypeExpr) *TypeExpr {
return &TypeExpr{ return &TypeExpr{
kind: KTypeExpr, kind: KTypeExpr,
+37 -29
View File
@@ -549,43 +549,47 @@ func (q *checker) bcheckWhile(n *a.While) error {
return nil return nil
} }
func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (nMin *big.Int, nMax *big.Int, retErr error) {
if depth > a.MaxExprDepth { if depth > a.MaxExprDepth {
return nil, nil, fmt.Errorf("check: expression recursion depth too large") return nil, nil, fmt.Errorf("check: expression recursion depth too large")
} }
depth++ depth++
if cv := n.ConstValue(); cv != nil {
return cv, cv, nil
}
// TODO: look in q.facts for "n == constantValue".
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { if cv := n.ConstValue(); cv != nil {
case 0: nMin, nMax = cv, cv
return q.bcheckExprOther(n, depth) } else {
case t.FlagsUnaryOp: // TODO: look in q.facts for "n == constantValue".
return q.bcheckExprUnaryOp(n, depth) switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case t.FlagsBinaryOp: case 0:
if n.ID0().Key() == t.KeyXBinaryAs { nMin, nMax, retErr = q.bcheckExprOther(n, depth)
lMin, lMax, err := q.bcheckExpr(n.LHS().Expr(), depth) case t.FlagsUnaryOp:
if err != nil { nMin, nMax, retErr = q.bcheckExprUnaryOp(n, depth)
return nil, nil, err case t.FlagsBinaryOp:
if n.ID0().Key() == t.KeyXBinaryAs {
nMin, nMax, retErr = q.bcheckExpr(n.LHS().Expr(), depth)
} else {
nMin, nMax, retErr = q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
} }
rMin, rMax, err := q.bcheckTypeExpr(n.RHS().TypeExpr()) case t.FlagsAssociativeOp:
if err != nil { nMin, nMax, retErr = q.bcheckExprAssociativeOp(n, depth)
return nil, nil, err
} default:
if lMin.Cmp(rMin) < 0 || lMax.Cmp(rMax) > 0 { return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
return nil, nil, fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
n.LHS().Expr().String(q.tm), lMin, lMax, rMin, rMax,
)
}
return lMin, lMax, nil
} }
return q.bcheckExprBinaryOp(n.LHS().Expr(), n.ID0().Key(), n.RHS().Expr(), depth)
case t.FlagsAssociativeOp:
return q.bcheckExprAssociativeOp(n, depth)
} }
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key()) if retErr != nil {
return nil, nil, retErr
}
tMin, tMax, err := q.bcheckTypeExpr(n.MType())
if err != nil {
return nil, nil, err
}
if (nMin != nil && tMin != nil && nMin.Cmp(tMin) < 0) || (nMax != nil && tMax != nil && nMax.Cmp(tMax) > 0) {
return nil, nil, fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
n.String(q.tm), nMin, nMax, tMin, tMax)
}
return nMin, nMax, nil
} }
func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
@@ -805,6 +809,10 @@ func (q *checker) bcheckExprAssociativeOp(n *a.Expr, depth uint32) (*big.Int, *b
} }
func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
if n.Eq(TypeExprIdeal) {
return nil, nil, nil
}
switch n.Decorator().Key() { switch n.Decorator().Key() {
case t.KeyPtr, t.KeyOpenBracket: case t.KeyPtr, t.KeyOpenBracket:
return nil, nil, nil return nil, nil, nil
+1 -1
View File
@@ -59,7 +59,7 @@ func TestCheck(t *testing.T) {
x = 0 x = 0
x = 1 + (x * 0) x = 1 + (x * 0)
y = -y - 1 y = -(y * 0) - 1
y = this.i y = this.i
b = not true b = not true
+5 -5
View File
@@ -343,7 +343,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err return err
} }
} }
n.SetMType(foo.MType()) n.SetMType(foo.MType().Unrefined())
return nil return nil
} }
@@ -490,7 +490,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
} }
n.SetConstValue(cv) n.SetConstValue(cv)
} }
n.SetMType(rTyp) n.SetMType(rTyp.Unrefined())
return nil return nil
case t.KeyXUnaryNot: case t.KeyXUnaryNot:
@@ -501,7 +501,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
if cv := rhs.ConstValue(); cv != nil { if cv := rhs.ConstValue(); cv != nil {
n.SetConstValue(btoi(cv.Cmp(zero) == 0)) n.SetConstValue(btoi(cv.Cmp(zero) == 0))
} }
n.SetMType(TypeExprBool) n.SetMType(TypeExprBool.Unrefined())
return nil return nil
} }
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key()) return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprUnaryOp", n.ID0().Key())
@@ -584,9 +584,9 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
if comparisonOps[0xFF&op.Key()] { if comparisonOps[0xFF&op.Key()] {
n.SetMType(TypeExprBool) n.SetMType(TypeExprBool)
} else if lTyp != TypeExprIdeal { } else if lTyp != TypeExprIdeal {
n.SetMType(lTyp) n.SetMType(lTyp.Unrefined())
} else { } else {
n.SetMType(rTyp) n.SetMType(rTyp.Unrefined())
} }
return nil return nil