diff --git a/lang/funcs/core/fmt/printf_func.go b/lang/funcs/core/fmt/printf_func.go index c998b76c..e6c3a41c 100644 --- a/lang/funcs/core/fmt/printf_func.go +++ b/lang/funcs/core/fmt/printf_func.go @@ -199,6 +199,11 @@ func (obj *PrintfFunc) Unify(expr interfaces.Expr) ([]interfaces.Invariant, erro invariants = append(invariants, invar) } + // catch situations like `printf("%d%d", 42)` + if len(cfavInvar.Args) <= i+1 { + return nil, fmt.Errorf("more specifiers (%d) than values (%d)", len(typList), len(cfavInvar.Args)-1) + } + // add the relationships to the called args invar = &interfaces.EqualityInvariant{ Expr1: cfavInvar.Args[i+1], @@ -557,6 +562,11 @@ func compileFormatToString(format string, values []types.Value) (string, error) } inType = false // done + if ix >= len(values) { + // programming error, probably in type unification + return "", fmt.Errorf("more specifiers (%d) than values (%d)", ix+1, len(values)) + } + // check the type (if not a variant) matches what we have... if typ == types.TypeVariant { if values[ix].Type() == nil { diff --git a/lang/interpret_test/TestAstFunc2/printfunificationerr0.txtar b/lang/interpret_test/TestAstFunc2/printfunificationerr0.txtar new file mode 100644 index 00000000..967055d8 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/printfunificationerr0.txtar @@ -0,0 +1,5 @@ +-- main.mcl -- +import "fmt" +test fmt.printf("%d%d", 42) {} # should not pass, missing second int +-- OUTPUT -- +# err: errUnify: only recursive solutions left