From cf7fbb80ba59327a37a24ba78064aa343a508431 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 12 Sep 2019 10:50:43 +0300 Subject: [PATCH] vm: fail if EQUAL has not enough arguments --- pkg/vm/vm.go | 6 ++++++ pkg/vm/vm_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index db03e6deb..71793c0fc 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -445,7 +445,13 @@ func (v *VM) execute(ctx *Context, op Instruction) { case EQUAL: b := v.estack.Pop() + if b == nil { + panic("no top-level element found") + } a := v.estack.Pop() + if a == nil { + panic("no second-to-the-top element found") + } v.estack.PushVal(reflect.DeepEqual(a, b)) // Bit operations. diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index a0682c1ab..1628c95b7 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -335,6 +335,32 @@ func TestDepth(t *testing.T) { assert.Equal(t, int64(3), vm.estack.Pop().BigInt().Int64()) } +func TestEQUALNoArguments(t *testing.T) { + prog := makeProgram(EQUAL) + vm := load(prog) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + +func TestEQUALBad1Argument(t *testing.T) { + prog := makeProgram(EQUAL) + vm := load(prog) + vm.estack.PushVal(1) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + +func TestEQUALGoodInteger(t *testing.T) { + prog := makeProgram(EQUAL) + vm := load(prog) + vm.estack.PushVal(5) + vm.estack.PushVal(5) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value) +} + func TestNumEqual(t *testing.T) { prog := makeProgram(NUMEQUAL) vm := load(prog)