From 44dfe8342dea58c3f652907511a8d805204737cf Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 29 May 2023 18:50:36 +0300 Subject: [PATCH] compiler: disallow named types redeclaration via contract config Signed-off-by: Anna Shaleva --- cli/smartcontract/generate_test.go | 3 +++ cli/smartcontract/testdata/invalid9/invalid.go | 12 ++++++++++++ cli/smartcontract/testdata/invalid9/invalid.yml | 16 ++++++++++++++++ pkg/compiler/compiler.go | 4 +++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 cli/smartcontract/testdata/invalid9/invalid.go create mode 100644 cli/smartcontract/testdata/invalid9/invalid.yml diff --git a/cli/smartcontract/generate_test.go b/cli/smartcontract/generate_test.go index e2a31d913..6c6a63f90 100644 --- a/cli/smartcontract/generate_test.go +++ b/cli/smartcontract/generate_test.go @@ -547,4 +547,7 @@ func TestCompile_GuessEventTypes(t *testing.T) { t.Run("extended types mismatch", func(t *testing.T) { check(t, filepath.Join("testdata", "invalid8"), "inconsistent usages of event `SomeEvent`: extended type of param #0 mismatch") }) + t.Run("named types redeclare", func(t *testing.T) { + check(t, filepath.Join("testdata", "invalid9"), "configured declared named type intersects with the contract's one: `invalid9.NamedStruct`") + }) } diff --git a/cli/smartcontract/testdata/invalid9/invalid.go b/cli/smartcontract/testdata/invalid9/invalid.go new file mode 100644 index 000000000..5036bca9f --- /dev/null +++ b/cli/smartcontract/testdata/invalid9/invalid.go @@ -0,0 +1,12 @@ +package invalid9 + +import "github.com/nspcc-dev/neo-go/pkg/interop/runtime" + +type NamedStruct struct { + SomeInt int +} + +func Main() NamedStruct { + runtime.Notify("SomeEvent", []interface{}{123}) + return NamedStruct{SomeInt: 123} +} diff --git a/cli/smartcontract/testdata/invalid9/invalid.yml b/cli/smartcontract/testdata/invalid9/invalid.yml new file mode 100644 index 000000000..40bbf66db --- /dev/null +++ b/cli/smartcontract/testdata/invalid9/invalid.yml @@ -0,0 +1,16 @@ +name: Test undeclared event +events: + - name: SomeEvent + parameters: + - name: p1 + type: Array + extendedtype: + base: Array + name: invalid9.NamedStruct +namedtypes: + invalid9.NamedStruct: + base: Array + name: invalid9.NamedStruct + fields: + - field: SomeInt + base: Integer diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 2b90ac07e..a7826a552 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -340,7 +340,9 @@ func CompileAndSave(src string, o *Options) ([]byte, error) { cfg.NamedTypes = di.NamedTypes } for name, et := range o.DeclaredNamedTypes { - // TODO: handle name conflict (it can happen due to invalid user input e.g.) + if _, ok := cfg.NamedTypes[name]; ok { + return nil, fmt.Errorf("configured declared named type intersects with the contract's one: `%s`", name) + } cfg.NamedTypes[name] = et } for _, e := range o.ContractEvents {