vm: fail if NIP has not enough arguments

This commit is contained in:
Evgenii Stratonikov 2019-09-12 11:09:23 +03:00
parent 6e4014547d
commit 1881adabb9
2 changed files with 23 additions and 3 deletions

View file

@ -398,9 +398,10 @@ func (v *VM) execute(ctx *Context, op Instruction) {
v.estack.PushVal(v.estack.Len())
case NIP:
elem := v.estack.Pop()
_ = v.estack.Pop()
v.estack.Push(elem)
elem := v.estack.RemoveAt(1)
if elem == nil {
panic("no second element found")
}
case OVER:
b := v.estack.Pop()

View file

@ -803,6 +803,25 @@ func TestOVERgood(t *testing.T) {
assert.Equal(t, 3, vm.estack.Len())
}
func TestNIPBadNoItem(t *testing.T) {
prog := makeProgram(NIP)
vm := load(prog)
vm.estack.PushVal(1)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestNIPGood(t *testing.T) {
prog := makeProgram(NIP)
vm := load(prog)
vm.estack.PushVal(1)
vm.estack.PushVal(2)
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 TestDROPBadNoItem(t *testing.T) {
prog := makeProgram(DROP)
vm := load(prog)