Skip to content

Commit

Permalink
interp: do not allow function declaration without body
Browse files Browse the repository at this point in the history
Such function declaration denotes either a linkname (an access to
an arbitrary, typically unexported symbol, solved by go compiler),
or a foreign C or assembly implementation of the body.

Those cases are not supported (or planned to be) by the interpreter.

Fixes #1431.
  • Loading branch information
mvertes committed Aug 3, 2022
1 parent d3fc5e9 commit 255b1cf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
fallthrough

case funcDecl:
// Do not allow function declarations without body.
if len(n.child) < 4 {
err = n.cfgErrorf("function declaration without body is unsupported (linkname or assembly can not be interpreted).")
return false
}
n.val = n
// Compute function type before entering local scope to avoid
// possible collisions with function argument names.
Expand Down
1 change: 1 addition & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ func TestEvalCall(t *testing.T) {
{src: ` test := func(a, b int) int { return a }
blah := func() (int, float64) { return 1, 1.1 }
a := test(blah())`, err: "3:15: cannot use func() (int,float64) as type (int,int)"},
{src: "func f()", err: "function declaration without body is unsupported"},
})
}

Expand Down

0 comments on commit 255b1cf

Please sign in to comment.