From 255b1cf1deb061ee9feac426ff92cba44a8d4527 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 3 Aug 2022 10:06:06 +0200 Subject: [PATCH] interp: do not allow function declaration without body 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. --- interp/cfg.go | 5 +++++ interp/interp_eval_test.go | 1 + 2 files changed, 6 insertions(+) diff --git a/interp/cfg.go b/interp/cfg.go index 1fe6b2271..78af0c7dd 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -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. diff --git a/interp/interp_eval_test.go b/interp/interp_eval_test.go index cc5b5eab8..602cd133f 100644 --- a/interp/interp_eval_test.go +++ b/interp/interp_eval_test.go @@ -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"}, }) }