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)
}
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 {
return &TypeExpr{
kind: KTypeExpr,
+37 -29
View File
@@ -549,43 +549,47 @@ func (q *checker) bcheckWhile(n *a.While) error {
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 {
return nil, nil, fmt.Errorf("check: expression recursion depth too large")
}
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) {
case 0:
return q.bcheckExprOther(n, depth)
case t.FlagsUnaryOp:
return q.bcheckExprUnaryOp(n, depth)
case t.FlagsBinaryOp:
if n.ID0().Key() == t.KeyXBinaryAs {
lMin, lMax, err := q.bcheckExpr(n.LHS().Expr(), depth)
if err != nil {
return nil, nil, err
if cv := n.ConstValue(); cv != nil {
nMin, nMax = cv, cv
} else {
// TODO: look in q.facts for "n == constantValue".
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0:
nMin, nMax, retErr = q.bcheckExprOther(n, depth)
case t.FlagsUnaryOp:
nMin, nMax, retErr = q.bcheckExprUnaryOp(n, depth)
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())
if err != nil {
return nil, nil, err
}
if lMin.Cmp(rMin) < 0 || lMax.Cmp(rMax) > 0 {
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
case t.FlagsAssociativeOp:
nMin, nMax, retErr = q.bcheckExprAssociativeOp(n, depth)
default:
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.ID0().Key())
}
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) {
@@ -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) {
if n.Eq(TypeExprIdeal) {
return nil, nil, nil
}
switch n.Decorator().Key() {
case t.KeyPtr, t.KeyOpenBracket:
return nil, nil, nil
+1 -1
View File
@@ -59,7 +59,7 @@ func TestCheck(t *testing.T) {
x = 0
x = 1 + (x * 0)
y = -y - 1
y = -(y * 0) - 1
y = this.i
b = not true
+5 -5
View File
@@ -343,7 +343,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
}
n.SetMType(foo.MType())
n.SetMType(foo.MType().Unrefined())
return nil
}
@@ -490,7 +490,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
}
n.SetConstValue(cv)
}
n.SetMType(rTyp)
n.SetMType(rTyp.Unrefined())
return nil
case t.KeyXUnaryNot:
@@ -501,7 +501,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
if cv := rhs.ConstValue(); cv != nil {
n.SetConstValue(btoi(cv.Cmp(zero) == 0))
}
n.SetMType(TypeExprBool)
n.SetMType(TypeExprBool.Unrefined())
return nil
}
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()] {
n.SetMType(TypeExprBool)
} else if lTyp != TypeExprIdeal {
n.SetMType(lTyp)
n.SetMType(lTyp.Unrefined())
} else {
n.SetMType(rTyp)
n.SetMType(rTyp.Unrefined())
}
return nil