Skip to content

Commit 0a9287c

Browse files
Merge pull request #21 from trailofbits/dev-exception-improvements
Improve exception handling
2 parents 117a110 + 49a118e commit 0a9287c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

pyevmasm/evmasm.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class UnknownMnemonicError(Exception):
3535
class UnknownOpcodeError(Exception):
3636
pass
3737

38+
class AssembleError(Exception):
39+
pass
40+
41+
class ParseError(Exception):
42+
pass
43+
3844

3945
class InstructionTable(dict):
4046
"""
@@ -365,7 +371,7 @@ def parse_operand(self, buf):
365371
operand |= next(buf)
366372
self._operand = operand
367373
except StopIteration:
368-
raise Exception("Not enough data for decoding")
374+
raise ParseError("Not enough data for decoding")
369375

370376
@property
371377
def operand_size(self):
@@ -557,8 +563,8 @@ def assemble_one(asmcode, pc=0):
557563
assert len(asmcode) == 2
558564
instr.operand = int(asmcode[1], 0)
559565
return instr
560-
except BaseException:
561-
raise Exception("Something wrong at pc %d" % pc)
566+
except:
567+
raise AssembleError("Something wrong at pc %d" % pc)
562568

563569

564570
def assemble_all(asmcode, pc=0):
@@ -627,10 +633,13 @@ def disassemble_one(bytecode, pc=0):
627633
instruction = instruction_table[opcode]
628634
instruction.pc = pc
629635

630-
if instruction.has_operand:
631-
instruction.parse_operand(bytecode)
632-
633-
return instruction
636+
try:
637+
if instruction.has_operand:
638+
instruction.parse_operand(bytecode)
639+
except ParseError:
640+
instruction = None
641+
finally:
642+
return instruction
634643

635644

636645
def disassemble_all(bytecode, pc=0):
@@ -672,7 +681,7 @@ def disassemble_all(bytecode, pc=0):
672681
while True:
673682
instr = disassemble_one(bytecode, pc=pc)
674683
if not instr:
675-
return
684+
raise StopIteration
676685
pc += instr.size
677686
yield instr
678687

0 commit comments

Comments
 (0)