From f0426ac2d52b7f84e5c35882cdf8c171523b1548 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 12 Sep 2019 12:02:38 +0300 Subject: [PATCH] vm: do nothing if SHL/SHR by 0 --- pkg/vm/vm.go | 6 ++++++ pkg/vm/vm_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 74a4057ae..a209a1382 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -505,11 +505,17 @@ func (v *VM) execute(ctx *Context, op Instruction) { case SHL: b := v.estack.Pop().BigInt() + if b.Int64() == 0 { + return + } a := v.estack.Pop().BigInt() v.estack.PushVal(new(big.Int).Lsh(a, uint(b.Int64()))) case SHR: b := v.estack.Pop().BigInt() + if b.Int64() == 0 { + return + } a := v.estack.Pop().BigInt() v.estack.PushVal(new(big.Int).Rsh(a, uint(b.Int64()))) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index f75631fb7..87ac78092 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -283,6 +283,50 @@ func TestSub(t *testing.T) { assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64()) } +func TestSHRGood(t *testing.T) { + prog := makeProgram(SHR) + vm := load(prog) + vm.estack.PushVal(4) + vm.estack.PushVal(2) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, makeStackItem(1), vm.estack.Pop().value) +} + +func TestSHRZero(t *testing.T) { + prog := makeProgram(SHR) + vm := load(prog) + vm.estack.PushVal([]byte{0, 1}) + vm.estack.PushVal(0) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value) +} + +func TestSHLGood(t *testing.T) { + prog := makeProgram(SHL) + vm := load(prog) + vm.estack.PushVal(4) + vm.estack.PushVal(2) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, makeStackItem(16), vm.estack.Pop().value) +} + +func TestSHLZero(t *testing.T) { + prog := makeProgram(SHL) + vm := load(prog) + vm.estack.PushVal([]byte{0, 1}) + vm.estack.PushVal(0) + vm.Run() + assert.Equal(t, false, vm.state.HasFlag(faultState)) + assert.Equal(t, 1, vm.estack.Len()) + assert.Equal(t, makeStackItem([]byte{0, 1}), vm.estack.Pop().value) +} + func TestLT(t *testing.T) { prog := makeProgram(LT) vm := load(prog)