vm: harden LEFT and RIGHT implementations against negative indexes

This commit is contained in:
Roman Khimov 2019-09-11 12:03:43 +03:00
parent 17f3a21e27
commit 8311bda355
2 changed files with 24 additions and 0 deletions

View file

@ -332,6 +332,9 @@ func (v *VM) execute(ctx *Context, op Instruction) {
v.estack.PushVal(s[o : o+l])
case LEFT:
l := int(v.estack.Pop().BigInt().Int64())
if l < 0 {
panic("negative length")
}
s := v.estack.Pop().Bytes()
if t := len(s); l > t {
l = t
@ -339,6 +342,9 @@ func (v *VM) execute(ctx *Context, op Instruction) {
v.estack.PushVal(s[:l])
case RIGHT:
l := int(v.estack.Pop().BigInt().Int64())
if l < 0 {
panic("negative length")
}
s := v.estack.Pop().Bytes()
v.estack.PushVal(s[len(s)-l:])
case XDROP:

View file

@ -925,6 +925,15 @@ func TestLEFTBadNoString(t *testing.T) {
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestLEFTBadNegativeLen(t *testing.T) {
prog := makeProgram(LEFT)
vm := load(prog)
vm.estack.PushVal([]byte("abcdef"))
vm.estack.PushVal(-1)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestLEFTGood(t *testing.T) {
prog := makeProgram(LEFT)
vm := load(prog)
@ -962,6 +971,15 @@ func TestRIGHTBadNoString(t *testing.T) {
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestRIGHTBadNegativeLen(t *testing.T) {
prog := makeProgram(RIGHT)
vm := load(prog)
vm.estack.PushVal([]byte("abcdef"))
vm.estack.PushVal(-1)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestRIGHTGood(t *testing.T) {
prog := makeProgram(RIGHT)
vm := load(prog)