diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 3e6914686..74a4057ae 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -759,10 +759,7 @@ func (v *VM) execute(ctx *Context, op Instruction) { case SIZE: elem := v.estack.Pop() - arr, ok := elem.value.Value().([]uint8) - if !ok { - panic("SIZE: item not of type []uint8") - } + arr := elem.Bytes() v.estack.PushVal(len(arr)) case JMP, JMPIF, JMPIFNOT: diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 2773a4bf2..f75631fb7 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -549,6 +549,33 @@ func TestPICKITEMByteArray(t *testing.T) { assert.Equal(t, makeStackItem(2), vm.estack.Pop().value) } +func TestSIZENoArgument(t *testing.T) { + prog := makeProgram(SIZE) + vm := load(prog) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + +func TestSIZEByteArray(t *testing.T) { + prog := makeProgram(SIZE) + vm := load(prog) + vm.estack.PushVal([]byte{0, 1}) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, makeStackItem(2), vm.estack.Pop().value) +} + +func TestSIZEBool(t *testing.T) { + prog := makeProgram(SIZE) + vm := load(prog) + vm.estack.PushVal(false) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, makeStackItem(1), vm.estack.Pop().value) +} + func TestSIGNNoArgument(t *testing.T) { prog := makeProgram(SIGN) vm := load(prog)