From 6b8c94e6c4c54cda951e54b5113988ed365fcc3c Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 4 Oct 2022 12:00:08 +0200 Subject: [PATCH] interp: check that send operate on channel value Not performing this check was leading to a panic at run-time. It now fails early with a compile error. Fixes #1453. --- interp/cfg.go | 9 ++++++++- interp/interp_eval_test.go | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/interp/cfg.go b/interp/cfg.go index e562e139c..c77f2c57f 100644 --- a/interp/cfg.go +++ b/interp/cfg.go @@ -960,7 +960,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string } wireChild(n) - case declStmt, exprStmt, sendStmt: + case sendStmt: + if !isChan(n.child[0].typ) { + err = n.cfgErrorf("invalid operation: cannot send to non-channel %s", n.child[0].typ.id()) + break + } + fallthrough + + case declStmt, exprStmt: wireChild(n) l := n.lastChild() n.findex = l.findex diff --git a/interp/interp_eval_test.go b/interp/interp_eval_test.go index 2b2ec58fd..89d963813 100644 --- a/interp/interp_eval_test.go +++ b/interp/interp_eval_test.go @@ -642,6 +642,8 @@ func TestEvalChan(t *testing.T) { return ok && msg == "ping" })()`, res: "true", }, + {src: `a :=5; a <- 4`, err: "cannot send to non-channel int"}, + {src: `a :=5; b := <-a`, err: "cannot receive from non-channel int"}, }) }