From 3f585d3b3ceac0a0111ca65490ac3376f639cde3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20W=C3=BCstholz?= Date: Wed, 22 Feb 2017 11:38:46 +0100 Subject: [PATCH] core/asm: address more code review feedback. --- core/asm/asm.go | 37 +++++++++++++++++++------------------ core/asm/asm_test.go | 22 ++++++++++++++++++++-- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/core/asm/asm.go b/core/asm/asm.go index 744c2dd00f..5fe827e7c1 100644 --- a/core/asm/asm.go +++ b/core/asm/asm.go @@ -26,20 +26,18 @@ import ( // Iterator for disassembled EVM instructions type instructionIterator struct { - code []byte - pc uint64 - arg []byte - op vm.OpCode - error error + code []byte + pc uint64 + arg []byte + op vm.OpCode + error error + started bool } // Create a new instruction iterator. func NewInstructionIterator(code []byte) *instructionIterator { it := new(instructionIterator) it.code = code - it.pc = 0 - it.error = nil - it.arg = nil return it } @@ -50,12 +48,15 @@ func (it *instructionIterator) Next() bool { return false } - if it.arg == nil { - // We are calling this procedure for the first time. - it.pc = 0 + if it.started { + // Since the iteration has been already started we move to the next instruction. + if it.arg != nil { + it.pc += uint64(len(it.arg)) + } + it.pc++ } else { - // We are moving to the next instruction. - it.pc += uint64(len(it.arg)) + 1 + // We start the iteration from the first instruction. + it.started = true } if uint64(len(it.code)) <= it.pc { @@ -73,7 +74,7 @@ func (it *instructionIterator) Next() bool { } it.arg = it.code[it.pc+1 : u] } else { - it.arg = make([]byte, 0) + it.arg = nil } return true } @@ -113,8 +114,8 @@ func PrintDisassembled(code string) error { fmt.Printf("%06v: %v\n", it.PC(), it.Op()) } } - if it.Error() != nil { - return it.Error() + if err := it.Error(); err != nil { + return err } return nil } @@ -131,8 +132,8 @@ func Disassemble(script []byte) ([]string, error) { instrs = append(instrs, fmt.Sprintf("%06v: %v\n", it.PC(), it.Op())) } } - if it.Error() != nil { - return nil, it.Error() + if err := it.Error(); err != nil { + return nil, err } return instrs, nil } diff --git a/core/asm/asm_test.go b/core/asm/asm_test.go index b0454921cd..92b26b67a5 100644 --- a/core/asm/asm_test.go +++ b/core/asm/asm_test.go @@ -32,8 +32,8 @@ func TestInstructionIteratorValid(t *testing.T) { cnt++ } - if it.Error() != nil { - t.Errorf("Expected 2, but encountered error %v instead.", it.Error()) + if err := it.Error(); err != nil { + t.Errorf("Expected 2, but encountered error %v instead.", err) } if cnt != 2 { t.Errorf("Expected 2, but got %v instead.", cnt) @@ -54,3 +54,21 @@ func TestInstructionIteratorInvalid(t *testing.T) { t.Errorf("Expected an error, but got %v instead.", cnt) } } + +// Tests disassembling the instructions for empty evm code +func TestInstructionIteratorEmpty(t *testing.T) { + cnt := 0 + script, _ := hex.DecodeString("") + + it := NewInstructionIterator(script) + for it.Next() { + cnt++ + } + + if err := it.Error(); err != nil { + t.Errorf("Expected 0, but encountered error %v instead.", err) + } + if cnt != 0 { + t.Errorf("Expected 0, but got %v instead.", cnt) + } +}