vm: truncate length in SUBSTR

Also fail on first error, without changing the stack.
This commit is contained in:
Evgenii Stratonikov 2019-09-18 14:19:58 +03:00
parent 2f9e7ca037
commit 7cd69ea8e2
2 changed files with 17 additions and 9 deletions

View file

@ -318,18 +318,22 @@ func (v *VM) execute(ctx *Context, op Instruction) {
v.estack.PushVal(ab)
case SUBSTR:
l := int(v.estack.Pop().BigInt().Int64())
o := int(v.estack.Pop().BigInt().Int64())
s := v.estack.Pop().Bytes()
if l < 0 {
panic("negative length")
}
o := int(v.estack.Pop().BigInt().Int64())
if o < 0 {
panic("negative index")
}
if l+o > len(s) {
panic("out of bounds access")
s := v.estack.Pop().Bytes()
if o > len(s) {
panic("invalid offset")
}
v.estack.PushVal(s[o : o+l])
last := l + o
if last > len(s) {
last = len(s)
}
v.estack.PushVal(s[o:last])
case LEFT:
l := int(v.estack.Pop().BigInt().Int64())
if l < 0 {

View file

@ -1138,20 +1138,22 @@ func TestSUBSTRBadOffset(t *testing.T) {
prog := makeProgram(SUBSTR)
vm := load(prog)
vm.estack.PushVal([]byte("abcdef"))
vm.estack.PushVal(6)
vm.estack.PushVal(7)
vm.estack.PushVal(1)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
}
func TestSUBSTRBadLen(t *testing.T) {
func TestSUBSTRBigLen(t *testing.T) {
prog := makeProgram(SUBSTR)
vm := load(prog)
vm.estack.PushVal([]byte("abcdef"))
vm.estack.PushVal(1)
vm.estack.PushVal(6)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, []byte("bcdef"), vm.estack.Pop().Bytes())
}
func TestSUBSTRBad387(t *testing.T) {
@ -1163,7 +1165,9 @@ func TestSUBSTRBad387(t *testing.T) {
vm.estack.PushVal(1)
vm.estack.PushVal(6)
vm.Run()
assert.Equal(t, true, vm.state.HasFlag(faultState))
assert.Equal(t, false, vm.state.HasFlag(faultState))
assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, []byte("bcdef"), vm.estack.Pop().Bytes())
}
func TestSUBSTRBadNegativeOffset(t *testing.T) {