ashdnazg / pyreshark

A Wireshark plugin providing a simple interface for writing dissectors in Python.
https://github.com/ashdnazg/pyreshark/releases/tag/0.1.4
170 stars 33 forks source link

maximum recursion depth exceeded on a simple code #7

Closed kimstik closed 10 years ago

kimstik commented 10 years ago

Pyreshark 0.1.3 / Win32 On a simple code

'''
@summary: A my test protocol...
'''
from cal.cal_types import ProtocolBase, FieldItem, PyFunctionItem, Subtree, TextItem
from cal.ws_consts import FT_UINT32, FT_UINT16, BASE_HEX, FT_UINT8, FT_ETHER, FT_IPv4

class Protocol(ProtocolBase):
    def __init__(self):
        self._name = "MyTest over TCP"
        self._filter_name = "mytest"
        self._short_name = "MYTEST"
        self._items = [FieldItem("header", FT_UINT32, "Mytest header", display = BASE_HEX),
                       Subtree(TextItem("Op", "Operation"), [PyFunctionItem(self.add_addresses, { "t1" : FieldItem("t1",     FT_UINT32, "T1"),
                                                                                                  "t2" : FieldItem("t2",     FT_UINT32, "T2"),
                                                                                                  "t3" : FieldItem("t3",     FT_UINT32, "T3"),
                                                                                                  "t4" : FieldItem("t4",     FT_UINT32, "T4"),
                                                                                                  "t5" : FieldItem("t5",     FT_UINT32, "T5"),
                                                                                                  "t6" : FieldItem("t6",     FT_UINT32, "T6"),
                                                                                                })]),
                       ]
        self._register_under = { "tcp.port": 16001}

    def add_addresses(self, packet):
        ptype = packet.unpack(">L",4)[0]
        packet.read_item("t1")
        packet.offset += 16
        packet.read_item("t2")
        packet.read_item("t3")

I've got errors on long files: Traceback (most recent call last): File "_ctypes/callbacks.c", line 314, in 'calling callback function' File "C:\Program Files\Wireshark\python\cal\cal_types.py", line 499, in _callback p = Packet(p_tvb_and_tree.contents.tvb, p_tvb_and_tree.contents.tree, p_pinfo, p_offset, self._cal, self._items_dict) RuntimeError: maximum recursion depth exceeded while calling a Python object Traceback (most recent call last): File "_ctypes/callbacks.c", line 314, in 'calling callback function' ......

ashdnazg commented 10 years ago

Can you please send me a capture file where this error occurs? (address in the readme)

ashdnazg commented 10 years ago

Thank you! What happened is quite fascinating, it seems you were trying to read values past the end of the packet, which triggered an internal exception in wireshark that somehow didn't return control to the python function, which means python thought the next packet's python function was called by the current python function and so on, until the stack reached its limit!

The solution is quite simple, check len(packet.buffer) versus packet.offset before reading items if you're not sure they exist.

My current philosophy is putting as little checks as possible in the python code, to reduce the performance toll as much as possible. It does have the drawback of having ambiguous errors with little or no relation to the actual problems.

ashdnazg commented 10 years ago

I don't think I will add detection of user errors during dissection.