From 723dcc6e25684a75f97ed4cec8b5db5c4bb7c045 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 11 Sep 2019 17:05:56 +0300 Subject: [PATCH 1/2] vm: make APPEND push no value on stack --- pkg/vm/vm.go | 8 ++++++-- pkg/vm/vm_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index d2638f740..a3c14149d 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -633,10 +633,14 @@ func (v *VM) execute(ctx *Context, op Instruction) { arrElem := v.estack.Pop() switch t := arrElem.value.(type) { - case *ArrayItem, *StructItem: + case *ArrayItem: arr := t.Value().([]StackItem) arr = append(arr, itemElem.value) - v.estack.PushVal(arr) + t.value = arr + case *StructItem: + arr := t.Value().([]StackItem) + arr = append(arr, itemElem.value) + t.value = arr case *ByteArrayItem: newVal := append(t.value, itemElem.value.Value().([]byte)...) v.estack.PushVal(newVal) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index a75d78b50..1a7b466b4 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -444,6 +444,41 @@ func TestNEWSTRUCTWrongType(t *testing.T) { assert.Equal(t, true, vm.state.HasFlag(faultState)) } +func TestAPPENDArray(t *testing.T) { + prog := makeProgram(DUP, PUSH5, APPEND) + vm := load(prog) + vm.estack.Push(&Element{value: &ArrayItem{}}) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, &ArrayItem{[]StackItem{makeStackItem(5)}}, vm.estack.Pop().value) +} + +func TestAPPENDStruct(t *testing.T) { + prog := makeProgram(DUP, PUSH5, APPEND) + vm := load(prog) + vm.estack.Push(&Element{value: &StructItem{}}) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, &StructItem{[]StackItem{makeStackItem(5)}}, vm.estack.Pop().value) +} + +func TestAPPENDBadNoArguments(t *testing.T) { + prog := makeProgram(APPEND) + vm := load(prog) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + +func TestAPPENDBad1Argument(t *testing.T) { + prog := makeProgram(APPEND) + vm := load(prog) + vm.estack.PushVal(1) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + func TestSIGNNoArgument(t *testing.T) { prog := makeProgram(SIGN) vm := load(prog) From 68c6c93980e9f593dbb9d4982de60b449b840296 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 11 Sep 2019 17:08:36 +0300 Subject: [PATCH 2/2] vm: do not allow APPEND to operate on bytearray --- pkg/vm/vm.go | 3 --- pkg/vm/vm_test.go | 9 +++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index a3c14149d..c9bc97d2b 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -641,9 +641,6 @@ func (v *VM) execute(ctx *Context, op Instruction) { arr := t.Value().([]StackItem) arr = append(arr, itemElem.value) t.value = arr - case *ByteArrayItem: - newVal := append(t.value, itemElem.value.Value().([]byte)...) - v.estack.PushVal(newVal) default: panic("APPEND: not of underlying type Array") } diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 1a7b466b4..5a7f5771a 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -479,6 +479,15 @@ func TestAPPENDBad1Argument(t *testing.T) { assert.Equal(t, true, vm.state.HasFlag(faultState)) } +func TestAPPENDWrongType(t *testing.T) { + prog := makeProgram(APPEND) + vm := load(prog) + vm.estack.PushVal([]byte{}) + vm.estack.PushVal(1) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + func TestSIGNNoArgument(t *testing.T) { prog := makeProgram(SIGN) vm := load(prog)