diff --git a/pkg/vm/stack_item.go b/pkg/vm/stack_item.go index 18ed92f19..6dff26040 100644 --- a/pkg/vm/stack_item.go +++ b/pkg/vm/stack_item.go @@ -75,6 +75,21 @@ func (i *StructItem) String() string { return "Struct" } +// Clone returns a Struct with all Struct fields copied by value. +// Array fields are still copied by reference. +func (i *StructItem) Clone() *StructItem { + ret := &StructItem{make([]StackItem, len(i.value))} + for j := range i.value { + switch t := i.value[j].(type) { + case *StructItem: + ret.value[j] = t.Clone() + default: + ret.value[j] = t + } + } + return ret +} + // BigIntegerItem represents a big integer on the stack. type BigIntegerItem struct { value *big.Int diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index f0647d8d6..b533b1347 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -642,14 +642,19 @@ func (v *VM) execute(ctx *Context, op Instruction) { itemElem := v.estack.Pop() arrElem := v.estack.Pop() + val := itemElem.value + if t, ok := itemElem.value.(*StructItem); ok { + val = t.Clone() + } + switch t := arrElem.value.(type) { case *ArrayItem: arr := t.Value().([]StackItem) - arr = append(arr, itemElem.value) + arr = append(arr, val) t.value = arr case *StructItem: arr := t.Value().([]StackItem) - arr = append(arr, itemElem.value) + arr = append(arr, val) t.value = arr default: panic("APPEND: not of underlying type Array") diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 2193afcb9..fd806b162 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -538,6 +538,18 @@ func TestAPPENDStruct(t *testing.T) { assert.Equal(t, &StructItem{[]StackItem{makeStackItem(5)}}, vm.estack.Pop().value) } +func TestAPPENDCloneStruct(t *testing.T) { + prog := makeProgram(DUP, PUSH0, NEWSTRUCT, TOALTSTACK, DUPFROMALTSTACK, APPEND, FROMALTSTACK, PUSH1, 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{ + &StructItem{[]StackItem{}}, + }}, vm.estack.Pop().value) +} + func TestAPPENDBadNoArguments(t *testing.T) { prog := makeProgram(APPEND) vm := load(prog)