vm: make PICKITEM accept bytearrays

This commit is contained in:
Evgenii Stratonikov 2019-09-12 11:19:25 +03:00
parent 1881adabb9
commit bafdb916a0
2 changed files with 37 additions and 1 deletions

View file

@ -691,7 +691,12 @@ func (v *VM) execute(ctx *Context, op Instruction) {
item := arr[index]
v.estack.PushVal(item)
default:
panic("PICKITEM: unknown type")
arr := obj.Bytes()
if index < 0 || index >= len(arr) {
panic("PICKITEM: invalid index")
}
item := arr[index]
v.estack.PushVal(int(item))
}
case SETITEM:

View file

@ -514,6 +514,37 @@ func TestAPPENDWrongType(t *testing.T) {
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestPICKITEMBadIndex(t *testing.T) {
prog := makeProgram(PICKITEM)
vm := load(prog)
vm.estack.PushVal([]StackItem{})
vm.estack.PushVal(0)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestPICKITEMArray(t *testing.T) {
prog := makeProgram(PICKITEM)
vm := load(prog)
vm.estack.PushVal([]StackItem{makeStackItem(1), makeStackItem(2)})
vm.estack.PushVal(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 TestPICKITEMByteArray(t *testing.T) {
prog := makeProgram(PICKITEM)
vm := load(prog)
vm.estack.PushVal([]byte{1, 2})
vm.estack.PushVal(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 TestSIGNNoArgument(t *testing.T) {
prog := makeProgram(SIGN)
vm := load(prog)