mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Bounds check call arguments more thoroughly
This commit is contained in:
1 parent
b92c782f95
commit
90f1fcc328
2 files changed
+53
-41
No files matched your search
+34
-14
@@ -447,7 +447,15 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lMin, lMax, err := q.bcheckTypeExpr(lhs.MType())
|
||||
return q.bcheckAssignment2(lhs, lhs.MType(), op, rhs)
|
||||
}
|
||||
|
||||
func (q *checker) bcheckAssignment2(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error {
|
||||
if lhs == nil && op != t.IDEq {
|
||||
return fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op.Key())
|
||||
}
|
||||
|
||||
lMin, lMax, err := q.bcheckTypeExpr(lTyp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -767,8 +775,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
// No-op.
|
||||
|
||||
case t.KeyOpenParen, t.KeyTry:
|
||||
_, _, err := q.bcheckExpr(n.LHS().Expr(), depth)
|
||||
if err != nil {
|
||||
if _, _, err := q.bcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -782,12 +789,6 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) ||
|
||||
isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySinceMark, 0) ||
|
||||
isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeyIsMarked, 0) ||
|
||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
||||
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_blocks", 2) ||
|
||||
isThisMethod(q.tm, n, "decode_huffman_slow", 2) || isThisMethod(q.tm, n, "decode_huffman_fast", 2) ||
|
||||
isThisMethod(q.tm, n, "init_fixed_huffman", 0) || isThisMethod(q.tm, n, "init_dynamic_huffman", 1) ||
|
||||
isThisMethod(q.tm, n, "init_huff", 4) ||
|
||||
isThatMethod(q.tm, n, t.KeyMark, 0) || isThatMethod(q.tm, n, t.KeyLimit, 1) ||
|
||||
isThatMethod(q.tm, n, t.KeySinceMark, 0) {
|
||||
|
||||
@@ -862,17 +863,18 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
if isThatMethod(q.tm, n, t.KeyLength, 0) || isThatMethod(q.tm, n, t.KeyAvailable, 0) {
|
||||
break
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
|
||||
|
||||
if err := q.bcheckExprCall(n, depth); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
case t.KeyOpenBracket:
|
||||
lhs := n.LHS().Expr()
|
||||
_, _, err := q.bcheckExpr(lhs, depth)
|
||||
if err != nil {
|
||||
if _, _, err := q.bcheckExpr(lhs, depth); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
rhs := n.RHS().Expr()
|
||||
_, _, err = q.bcheckExpr(rhs, depth)
|
||||
if err != nil {
|
||||
if _, _, err := q.bcheckExpr(rhs, depth); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@@ -954,6 +956,24 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
return q.bcheckTypeExpr(n.MType())
|
||||
}
|
||||
|
||||
func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
// TODO: handle func pre/post conditions.
|
||||
//
|
||||
// TODO: bcheck the receiver, e.g. ptr vs nptr.
|
||||
lhs := n.LHS().Expr()
|
||||
f, err := q.resolveFunc(lhs.MType())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inFields := f.In().Fields()
|
||||
for i, o := range n.Args() {
|
||||
if err := q.bcheckAssignment2(nil, inFields[i].Field().XType(), t.IDEq, o.Arg().Value()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func makeSliceLengthExpr(slice *a.Expr) *a.Expr {
|
||||
x := a.NewExpr(a.FlagsTypeChecked, t.IDDot, t.IDLength, slice.Node(), nil, nil, nil)
|
||||
x.SetMType(typeExprPlaceholder) // HACK.
|
||||
|
||||
+19
-27
@@ -677,22 +677,30 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.ID0().Key(), n.Str(q.tm))
|
||||
}
|
||||
|
||||
func (q *checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
|
||||
// TODO: look up funcs in used packages.
|
||||
f := q.c.funcs[t.QID{typ.Receiver().Name(), typ.Name()}]
|
||||
if f.Func == nil {
|
||||
return nil, fmt.Errorf("check: cannot look up %q", typ.Str(q.tm))
|
||||
}
|
||||
return f.Func, nil
|
||||
}
|
||||
|
||||
func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||
lhs := n.LHS().Expr()
|
||||
if err := q.tcheckExpr(lhs, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp := n.LHS().Expr().MType()
|
||||
// TODO: look up funcs in used packages. Factor out a resolveFunc method?
|
||||
f := q.c.funcs[t.QID{lTyp.Receiver().Name(), lTyp.Name()}]
|
||||
if f.Func == nil {
|
||||
return fmt.Errorf("check: cannot look up %q", lTyp.Str(q.tm))
|
||||
f, err := q.resolveFunc(lhs.MType())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check that the func's in type matches the arguments.
|
||||
inFields := f.Func.In().Fields()
|
||||
inFields := f.In().Fields()
|
||||
if len(inFields) != len(n.Args()) {
|
||||
return fmt.Errorf("check: %q has %d arguments but %d were given",
|
||||
lTyp.Str(q.tm), len(inFields), len(n.Args()))
|
||||
lhs.MType().Str(q.tm), len(inFields), len(n.Args()))
|
||||
}
|
||||
for i, o := range n.Args() {
|
||||
// TODO: inline tcheckArg here, after removing the special-cased hacks
|
||||
@@ -709,8 +717,8 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
if n.ID0().Key() == t.KeyTry {
|
||||
n.SetMType(typeExprStatus)
|
||||
} else {
|
||||
// TODO: figure out how to translate f.Func.Out(), which is an
|
||||
// *ast.Struct, to an *ast.TypeExpr we can pass to n.SetMType.
|
||||
// TODO: figure out how to translate f.Out(), which is an *ast.Struct,
|
||||
// to an *ast.TypeExpr we can pass to n.SetMType.
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
}
|
||||
return nil
|
||||
@@ -760,23 +768,7 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyIn
|
||||
}
|
||||
|
||||
func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
// TODO: check that n.Args() is "(src:in.src)".
|
||||
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
|
||||
return false
|
||||
}
|
||||
if len(n.Args()) != nArgs {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName(methodName) {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
|
||||
}
|
||||
|
||||
// isThatMethod is like isThisMethod but for foo.bar(etc), not this.bar(etc).
|
||||
// isThatMethod matches foo.methodName(etc) where etc has nArgs elements.
|
||||
func isThatMethod(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
|
||||
return false
|
||||
|
||||
Reference in new issue
Block a user