From dbb49d02117ee51e0a77de6e935600eb005cb804 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 4 Sep 2017 19:53:02 +0200 Subject: pkg/compiler: prohibit arrays of size 0 This is pointless and the only case that can yield 0 static type size. --- pkg/compiler/testdata/errors.txt | 3 +++ pkg/compiler/types.go | 6 ++++++ 2 files changed, 9 insertions(+) (limited to 'pkg') diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index c339dcb80..629c2c864 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -110,6 +110,9 @@ foo$45(a int32) len[b] ### len can't be syscall return foo$46(a ptr[in, in]) ### unknown type in foo$47(a int32:2) ### unexpected ':', only struct fields can be bitfields foo$48(a ptr[in, int32:7]) ### unexpected ':', only struct fields can be bitfields +foo$49(a ptr[in, array[int32, 0:1]]) +foo$50(a ptr[in, array[int32, 0]]) ### arrays of size 0 are not supported +foo$51(a ptr[in, array[int32, 0:0]]) ### arrays of size 0 are not supported opt { ### struct uses reserved name opt f1 int32 diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index 29ef4dbf5..9f8ec7754 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -97,6 +97,12 @@ var typeArray = &typeDesc{ CantBeOpt: true, OptArgs: 1, Args: []namedArg{{"type", typeArgType}, {"size", typeArgRange}}, + Check: func(comp *compiler, t *ast.Type, args []*ast.Type, base sys.IntTypeCommon) { + if len(args) > 1 && args[1].Value == 0 && args[1].Value2 == 0 { + // This is the only case that can yield 0 static type size. + comp.error(args[1].Pos, "arrays of size 0 are not supported") + } + }, Gen: func(comp *compiler, t *ast.Type, args []*ast.Type, base sys.IntTypeCommon) sys.Type { elemType := comp.genType(args[0], "", base.ArgDir, false) kind, begin, end := sys.ArrayRandLen, uint64(0), uint64(0) -- cgit mrf-deployment