From 21efcb012bee088f7226e3d10dfbc70edf30aff0 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 17 Dec 2019 17:20:44 +0300 Subject: [PATCH] vm: fix non-dupped items in PICKITEM TestPICKITEMDupMap and TestPICKITEMDupArray were failing. --- pkg/vm/vm.go | 4 ++-- pkg/vm/vm_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 825780737..93f190c10 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -958,14 +958,14 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro if index < 0 || index >= len(arr) { panic("PICKITEM: invalid index") } - item := arr[index] + item := arr[index].Dup() v.estack.PushVal(item) case *MapItem: if !t.Has(key.value) { panic("invalid key") } k := toMapKey(key.value) - v.estack.Push(&Element{value: t.value[k]}) + v.estack.Push(&Element{value: t.value[k].Dup()}) default: arr := obj.Bytes() if index < 0 || index >= len(arr) { diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index ef701a792..b3fb9f81a 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -1092,6 +1092,30 @@ func TestPICKITEMByteArray(t *testing.T) { assert.Equal(t, makeStackItem(2), vm.estack.Pop().value) } +func TestPICKITEMDupArray(t *testing.T) { + prog := makeProgram(opcode.DUP, opcode.PUSH0, opcode.PICKITEM, opcode.ABS) + vm := load(prog) + vm.estack.PushVal([]StackItem{makeStackItem(-1)}) + runVM(t, vm) + assert.Equal(t, 2, vm.estack.Len()) + assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64()) + items := vm.estack.Pop().Value().([]StackItem) + assert.Equal(t, big.NewInt(-1), items[0].Value()) +} + +func TestPICKITEMDupMap(t *testing.T) { + prog := makeProgram(opcode.DUP, opcode.PUSHBYTES1, 42, opcode.PICKITEM, opcode.ABS) + vm := load(prog) + m := NewMapItem() + m.Add(makeStackItem([]byte{42}), makeStackItem(-1)) + vm.estack.Push(&Element{value: m}) + runVM(t, vm) + assert.Equal(t, 2, vm.estack.Len()) + assert.Equal(t, int64(1), vm.estack.Pop().BigInt().Int64()) + items := vm.estack.Pop().Value().(map[interface{}]StackItem) + assert.Equal(t, big.NewInt(-1), items[string([]byte{42})].Value()) +} + func TestPICKITEMMap(t *testing.T) { prog := makeProgram(opcode.PICKITEM) vm := load(prog)