vm: disallow negative offset in (*Context).Next()

Currently the only known reason this can happen is processing
ENDFINALLY opcode before the corresponding ENDTRY.

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2022-03-17 18:38:31 +03:00
parent f056c9aea8
commit 492c91b4c5
2 changed files with 14 additions and 0 deletions

View file

@ -95,6 +95,9 @@ func (c *Context) Next() (opcode.Opcode, []byte, error) {
var err error var err error
c.ip = c.nextip c.ip = c.nextip
if c.ip < 0 {
return 0, nil, errors.New("invalid instruction offset")
}
if c.ip >= len(c.prog) { if c.ip >= len(c.prog) {
return opcode.RET, nil, nil return opcode.RET, nil, nil
} }

View file

@ -1385,6 +1385,17 @@ func TestKEYS(t *testing.T) {
t.Run("WrongType", getTestFuncForVM(prog, nil, []stackitem.Item{})) t.Run("WrongType", getTestFuncForVM(prog, nil, []stackitem.Item{}))
} }
func TestTry_ENDFINALLY_before_ENDTRY(t *testing.T) {
prog := makeProgram(opcode.TRY, 0, 3, opcode.ENDFINALLY)
require.NoError(t, IsScriptCorrect(prog, nil))
v := load(prog)
var err error
require.NotPanics(t, func() { err = v.Run() })
require.Error(t, err)
}
func TestVALUESMap(t *testing.T) { func TestVALUESMap(t *testing.T) {
prog := makeProgram(opcode.VALUES) prog := makeProgram(opcode.VALUES)
vm := load(prog) vm := load(prog)