From ac93d7e1d83ffac19983e9d754a47e7808f1230e Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 28 Nov 2017 14:27:28 +0100 Subject: pkg/compiler: add check that len does not refer to array with varlen elements This [almost] always means a bug in descriptions. Fix all bugs identified by the check. --- pkg/compiler/check.go | 15 +++++++++++++++ pkg/compiler/testdata/errors2.txt | 2 ++ 2 files changed, 17 insertions(+) (limited to 'pkg/compiler') diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 5fefeaf68..349dd9017 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -221,6 +221,21 @@ func (comp *compiler) checkLenTarget(t *ast.Type, name, target string, fields [] if fld.Type == t { comp.error(t.Pos, "%v target %v refer to itself", t.Ident, target) } + if t.Ident == "len" { + inner := fld.Type + desc, args, _ := comp.getArgsBase(inner, "", prog.DirIn, false) + for desc == typePtr { + if desc != typePtr { + break + } + inner = args[1] + desc, args, _ = comp.getArgsBase(inner, "", prog.DirIn, false) + } + if desc == typeArray && comp.isVarlen(args[0]) { + comp.error(t.Pos, "len target %v refer to an array with"+ + " variable-size elements (do you mean bytesize?)", target) + } + } return } for _, parent := range parents { diff --git a/pkg/compiler/testdata/errors2.txt b/pkg/compiler/testdata/errors2.txt index 0292aca5e..5b418ab55 100644 --- a/pkg/compiler/testdata/errors2.txt +++ b/pkg/compiler/testdata/errors2.txt @@ -52,6 +52,8 @@ foo$104(a len[parent]) ### len target parent does not exist foo$105(a ptr[in, int32], b ptr[in, array[len[a, int32]]]) foo$106(a int32, b ptr[in, csum[a, inet, int32]]) foo$107(a int32, b ptr[in, csum[c, inet, int32]]) ### csum target c does not exist +foo$108(a ptr[in, array[string]], b len[a]) ### len target a refer to an array with variable-size elements (do you mean bytesize?) +foo$109(a ptr[in, array[string]], b ptr[in, len[a, int32]]) ### len target a refer to an array with variable-size elements (do you mean bytesize?) s1 { f1 len[s2, int32] ### len target s2 does not exist -- cgit mrf-deployment