Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,47 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
vm.push(a.(string) == b.(string))

case OpJump:
if arg < 0 {
panic("negative jump offset is invalid")
}
vm.ip += arg

case OpJumpIfTrue:
if arg < 0 {
panic("negative jump offset is invalid")
}
if vm.current().(bool) {
vm.ip += arg
}

case OpJumpIfFalse:
if arg < 0 {
panic("negative jump offset is invalid")
}
if !vm.current().(bool) {
vm.ip += arg
}

case OpJumpIfNil:
if arg < 0 {
panic("negative jump offset is invalid")
}
if runtime.IsNil(vm.current()) {
vm.ip += arg
}

case OpJumpIfNotNil:
if arg < 0 {
panic("negative jump offset is invalid")
}
if !runtime.IsNil(vm.current()) {
vm.ip += arg
}

case OpJumpIfEnd:
if arg < 0 {
panic("negative jump offset is invalid")
}
scope := vm.scope()
if scope.Index >= scope.Len {
vm.ip += arg
Expand Down
31 changes: 31 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1331,3 +1331,34 @@ func TestVM_Limits(t *testing.T) {
})
}
}

func TestVM_OpJump_NegativeOffset(t *testing.T) {
program := vm.NewProgram(
file.Source{},
nil,
nil,
0,
nil,
[]vm.Opcode{
vm.OpInt,
vm.OpInt,
vm.OpJump,
vm.OpInt,
vm.OpJump,
},
[]int{
1,
2,
-2, // negative offset for a forward jump opcode
3,
-2,
},
nil,
nil,
nil,
)

_, err := vm.Run(program, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "negative jump offset is invalid")
}
Loading