vm: harden LEFT and RIGHT implementations against negative indexes
This commit is contained in:
parent
17f3a21e27
commit
8311bda355
2 changed files with 24 additions and 0 deletions
|
@ -332,6 +332,9 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
v.estack.PushVal(s[o : o+l])
|
v.estack.PushVal(s[o : o+l])
|
||||||
case LEFT:
|
case LEFT:
|
||||||
l := int(v.estack.Pop().BigInt().Int64())
|
l := int(v.estack.Pop().BigInt().Int64())
|
||||||
|
if l < 0 {
|
||||||
|
panic("negative length")
|
||||||
|
}
|
||||||
s := v.estack.Pop().Bytes()
|
s := v.estack.Pop().Bytes()
|
||||||
if t := len(s); l > t {
|
if t := len(s); l > t {
|
||||||
l = t
|
l = t
|
||||||
|
@ -339,6 +342,9 @@ func (v *VM) execute(ctx *Context, op Instruction) {
|
||||||
v.estack.PushVal(s[:l])
|
v.estack.PushVal(s[:l])
|
||||||
case RIGHT:
|
case RIGHT:
|
||||||
l := int(v.estack.Pop().BigInt().Int64())
|
l := int(v.estack.Pop().BigInt().Int64())
|
||||||
|
if l < 0 {
|
||||||
|
panic("negative length")
|
||||||
|
}
|
||||||
s := v.estack.Pop().Bytes()
|
s := v.estack.Pop().Bytes()
|
||||||
v.estack.PushVal(s[len(s)-l:])
|
v.estack.PushVal(s[len(s)-l:])
|
||||||
case XDROP:
|
case XDROP:
|
||||||
|
|
|
@ -925,6 +925,15 @@ func TestLEFTBadNoString(t *testing.T) {
|
||||||
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
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) {
|
func TestLEFTGood(t *testing.T) {
|
||||||
prog := makeProgram(LEFT)
|
prog := makeProgram(LEFT)
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
@ -962,6 +971,15 @@ func TestRIGHTBadNoString(t *testing.T) {
|
||||||
assert.Equal(t, true, vm.state.HasFlag(faultState))
|
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) {
|
func TestRIGHTGood(t *testing.T) {
|
||||||
prog := makeProgram(RIGHT)
|
prog := makeProgram(RIGHT)
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
|
Loading…
Reference in a new issue