vm: fail if EQUAL has not enough arguments

This commit is contained in:
Evgenii Stratonikov 2019-09-12 10:50:43 +03:00
parent d257044298
commit cf7fbb80ba
2 changed files with 32 additions and 0 deletions

View file

@ -445,7 +445,13 @@ func (v *VM) execute(ctx *Context, op Instruction) {
case EQUAL: case EQUAL:
b := v.estack.Pop() b := v.estack.Pop()
if b == nil {
panic("no top-level element found")
}
a := v.estack.Pop() a := v.estack.Pop()
if a == nil {
panic("no second-to-the-top element found")
}
v.estack.PushVal(reflect.DeepEqual(a, b)) v.estack.PushVal(reflect.DeepEqual(a, b))
// Bit operations. // Bit operations.

View file

@ -335,6 +335,32 @@ func TestDepth(t *testing.T) {
assert.Equal(t, int64(3), vm.estack.Pop().BigInt().Int64()) 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) { func TestNumEqual(t *testing.T) {
prog := makeProgram(NUMEQUAL) prog := makeProgram(NUMEQUAL)
vm := load(prog) vm := load(prog)