Ghidrathon icon indicating copy to clipboard operation
Ghidrathon copied to clipboard

for loop throws Python TypeError for some Iterable objects

Open colton-gabertan opened this issue 1 year ago • 0 comments

Description

Most iterable objects returned by Ghidra API calls are iterable via classic for loops; however, there are some cases in which the iterable object is not compatible.

Steps to Re-create

def extract_function_loop(fh: FunctionHandle):
    f: ghidra.program.database.function.FunctionDB = fh.inner

    edges = []
    for block in SimpleBlockIterator(BasicBlockModel(currentProgram()), f.getBody(), monitor()):  # type: ignore [name-defined] # noqa: F821
        dests = block.getDestinations(monitor())  # type: ignore [name-defined] # noqa: F821
        s_addrs = block.getStartAddresses()

        while dests.hasNext():  # For loop throws Python TypeError
            for addr in s_addrs:
                edges.append((addr.getOffset(), dests.next().getDestinationAddress().getOffset()))

    if loops.has_loop(edges):
        yield Characteristic("loop"), AbsoluteVirtualAddress(f.getEntryPoint().getOffset())

https://github.com/mandiant/capa/blob/70d36ab6406a46b5bc9c2cac3866a80a8c8ea38d/capa/features/extractors/ghidra/function.py#L28C1-L42C1

Expected Behavior

dests should be compatible to use with a classic for loop i.e. for dest in dests:

Actual Behavior

Using a classic for loop throws a Python TypeError

image

Version Info

Ghidra v10.3.2
Ghidrathon v3.0.0

colton-gabertan avatar Aug 23 '23 21:08 colton-gabertan