vm: do nothing if SHL/SHR by 0

This commit is contained in:
Evgenii Stratonikov 2019-09-12 12:02:38 +03:00
parent 9eca5ee317
commit f0426ac2d5
2 changed files with 50 additions and 0 deletions

View file

@ -505,11 +505,17 @@ func (v *VM) execute(ctx *Context, op Instruction) {
case SHL: case SHL:
b := v.estack.Pop().BigInt() b := v.estack.Pop().BigInt()
if b.Int64() == 0 {
return
}
a := v.estack.Pop().BigInt() a := v.estack.Pop().BigInt()
v.estack.PushVal(new(big.Int).Lsh(a, uint(b.Int64()))) v.estack.PushVal(new(big.Int).Lsh(a, uint(b.Int64())))
case SHR: case SHR:
b := v.estack.Pop().BigInt() b := v.estack.Pop().BigInt()
if b.Int64() == 0 {
return
}
a := v.estack.Pop().BigInt() a := v.estack.Pop().BigInt()
v.estack.PushVal(new(big.Int).Rsh(a, uint(b.Int64()))) v.estack.PushVal(new(big.Int).Rsh(a, uint(b.Int64())))

View file

@ -283,6 +283,50 @@ func TestSub(t *testing.T) {
assert.Equal(t, int64(2), vm.estack.Pop().BigInt().Int64()) 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) { func TestLT(t *testing.T) {
prog := makeProgram(LT) prog := makeProgram(LT)
vm := load(prog) vm := load(prog)