From 71cfd14b9285359db55d3f793f9bf1bb58cba9c1 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 12 Sep 2019 11:24:10 +0300 Subject: [PATCH] vm: create an array of `false` items in NEWARRAY/NEWSTRUCT --- pkg/vm/vm.go | 12 ++++++++++-- pkg/vm/vm_test.go | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 6bb1efe39..b9c667c69 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -611,7 +611,7 @@ func (v *VM) execute(ctx *Context, op Instruction) { switch t := item.value.(type) { case *BigIntegerItem: n := t.value.Int64() - items := make([]StackItem, n) + items := makeArrayOfFalses(int(n)) v.estack.PushVal(&ArrayItem{items}) case *StructItem: v.estack.PushVal(&ArrayItem{t.value}) @@ -626,7 +626,7 @@ func (v *VM) execute(ctx *Context, op Instruction) { switch t := item.value.(type) { case *BigIntegerItem: n := t.value.Int64() - items := make([]StackItem, n) + items := makeArrayOfFalses(int(n)) v.estack.PushVal(&StructItem{items}) case *ArrayItem: v.estack.PushVal(&StructItem{t.value}) @@ -853,6 +853,14 @@ func (v *VM) execute(ctx *Context, op Instruction) { } } +func makeArrayOfFalses(n int) []StackItem { + items := make([]StackItem, n) + for i := range items { + items[i] = &BoolItem{false} + } + return items +} + func init() { log.SetPrefix("NEO-GO-VM > ") log.SetFlags(0) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index c45c2f0cb..d813ebc89 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -397,7 +397,7 @@ func TestNEWARRAYInteger(t *testing.T) { vm.Run() assert.Equal(t, false, vm.state.HasFlag(faultState)) assert.Equal(t, 1, vm.estack.Len()) - assert.Equal(t, &ArrayItem{make([]StackItem, 1)}, vm.estack.Pop().value) + assert.Equal(t, &ArrayItem{[]StackItem{makeStackItem(false)}}, vm.estack.Pop().value) } func TestNEWARRAYStruct(t *testing.T) { @@ -437,7 +437,7 @@ func TestNEWSTRUCTInteger(t *testing.T) { vm.Run() assert.Equal(t, false, vm.state.HasFlag(faultState)) assert.Equal(t, 1, vm.estack.Len()) - assert.Equal(t, &StructItem{make([]StackItem, 1)}, vm.estack.Pop().value) + assert.Equal(t, &StructItem{[]StackItem{makeStackItem(false)}}, vm.estack.Pop().value) } func TestNEWSTRUCTArray(t *testing.T) {