From a2a8981979f83e9a28650d12e0eaa123a83c181e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Mon, 9 Sep 2019 12:07:03 +0300 Subject: [PATCH] vm: fix not failing PUSHBYTES* on short read Add some tests also. Fixes #361. --- pkg/vm/vm.go | 3 +++ pkg/vm/vm_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 216f6ca6c..1fa1fe3c7 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -242,6 +242,9 @@ func (v *VM) execute(ctx *Context, op Instruction) { if op >= PUSHBYTES1 && op <= PUSHBYTES75 { b := ctx.readBytes(int(op)) + if b == nil { + panic("failed to read instruction parameter") + } v.estack.PushVal(b) return } diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 5324581e4..a27f53b6a 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -60,6 +60,22 @@ func TestPushBytes1to75(t *testing.T) { } } +func TestPushBytesNoParam(t *testing.T) { + prog := make([]byte, 1) + prog[0] = byte(PUSHBYTES1) + vm := load(prog) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + +func TestPushBytesShort(t *testing.T) { + prog := make([]byte, 10) + prog[0] = byte(PUSHBYTES10) // but only 9 left in the `prog` + vm := load(prog) + vm.Run() + assert.Equal(t, true, vm.state.HasFlag(faultState)) +} + func TestPushm1to16(t *testing.T) { var prog []byte for i := int(PUSHM1); i <= int(PUSH16); i++ {