From 0b58ed4a2233ab3e3d22ef77cf2ad8de3a2ce0f9 Mon Sep 17 00:00:00 2001 From: Evgenii <evgeniy@nspcc.ru> Date: Mon, 9 Sep 2019 17:05:40 +0300 Subject: [PATCH] vm: do not fault on LEFT with big index Fixes #378. --- pkg/vm/vm.go | 3 +++ pkg/vm/vm_test.go | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index d964d2178..f5a24ad3b 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -315,6 +315,9 @@ func (v *VM) execute(ctx *Context, op Instruction) { case LEFT: l := int(v.estack.Pop().BigInt().Int64()) s := v.estack.Pop().Bytes() + if t := len(s); l > t { + l = t + } v.estack.PushVal(s[:l]) case RIGHT: l := int(v.estack.Pop().BigInt().Int64()) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 78a9b1429..9eb26e22b 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -685,13 +685,15 @@ func TestLEFTGood(t *testing.T) { assert.Equal(t, []byte("ab"), vm.estack.Peek(0).Bytes()) } -func TestLEFTBadLen(t *testing.T) { +func TestLEFTGoodLen(t *testing.T) { prog := makeProgram(LEFT) vm := load(prog) vm.estack.PushVal([]byte("abcdef")) vm.estack.PushVal(8) 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("abcdef"), vm.estack.Peek(0).Bytes()) } func TestRIGHTBadNoArgs(t *testing.T) {