# Buggy code
def _process_packet(self, pkt):
    """Process each packet: matches the TCP seq/ack numbers
    to follow the TCP streams, and orders the fragments.
    """
    if self.app:
        # Special mode: Application layer. Use on top of TCP
        pay_class = pkt.__class__
        if not hasattr(pay_class, "tcp_reassemble"):
            # Cannot tcp-reassemble
            return pkt
        self.data += bytes(pkt)
        pkt = pay_class.tcp_reassemble(self.data, self.metadata)
        if pkt:
            self.data = b""
            self.metadata = {}
            return pkt
        return

    from scapy.layers.inet import IP, TCP

    if not pkt or TCP not in pkt:
        return pkt
    pay = pkt[TCP].payload
    if isinstance(pay, (NoPayload, conf.padding_layer)):
        return pkt
    new_data = pay.original
    # Match packets by a uniqute TCP identifier
    seq = pkt[TCP].seq
    ident = pkt.sprintf(self.fmt)
    data, metadata = self.tcp_frags[ident]
    # Let's guess which class is going to be used
    if "pay_class" not in metadata:
        pay_class = pay.__class__
        if not hasattr(pay_class, "tcp_reassemble"):
            # Cannot tcp-reassemble
            return pkt
        metadata["pay_class"] = pay_class
    else:
        pay_class = metadata["pay_class"]
    # Get a relative sequence number for a storage purpose
    relative_seq = metadata.get("relative_seq", None)
    if relative_seq is None:
        relative_seq = metadata["relative_seq"] = seq - 1
    seq = seq - relative_seq
    # Add the data to the buffer
    # Note that this take care of retransmission packets.
    data.append(new_data, seq)
    # Check TCP FIN or TCP RESET
    if pkt[TCP].flags.F or pkt[TCP].flags.R:
        metadata["tcp_end"] = True

    # In case any app layer protocol requires it,
    # allow the parser to inspect TCP PSH flag
    if pkt[TCP].flags.P:
        metadata["tcp_psh"] = True
    # XXX TODO: check that no empty space is missing in the buffer.
    # XXX Currently, if a TCP fragment was missing, we won't notice it.
    packet = None
    if data.full():
        # Reassemble using all previous packets
        packet = pay_class.tcp_reassemble(bytes(data), metadata)
    # Stack the result on top of the previous frames
    if packet:
        data.clear()
        del self.tcp_frags[ident]
        pay.underlayer.remove_payload()
        if IP in pkt:
            pkt[IP].len = None
            pkt[IP].chksum = None
        return pkt / packet


# Traceback (TypeError)
Traceback (most recent call last):
File "utils/poc.py", line 51, in <module>
test_failed_fn()
File "utils/poc.py", line 47, in test_failed_fn
extract_tls_payloads(os.path.join(data_root, fn), 900)
File "utils/poc.py", line 19, in extract_tls_payloads
payload = bytes(p[TLS])
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/layers/tls/session.py", line 966, in __bytes__
built_packet = super(_GenericTLSSessionInheritance, self).__bytes__()
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/packet.py", line 492, in __bytes__
return self.build()
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/packet.py", line 612, in build
p = self.do_build()
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/packet.py", line 594, in do_build
pkt = self.self_build()
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/packet.py", line 575, in self_build
p = f.addfield(self, p, val)
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/layers/tls/record.py", line 207, in addfield
res += self.i2m(pkt, p)
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/layers/tls/record.py", line 194, in i2m
cur = raw(p)
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/compat.py", line 53, in raw
return bytes(x)
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/layers/tls/session.py", line 966, in __bytes__
built_packet = super(_GenericTLSSessionInheritance, self).__bytes__()
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/packet.py", line 492, in __bytes__
return self.build()
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/layers/tls/handshake.py", line 1028, in build
m = s.client_random + s.server_random + raw(p)
File "/data1/wangqian/venv_py3/lib/python3.7/site-packages/scapy/compat.py", line 53, in raw
return bytes(x)
TypeError: 'NoneType' object cannot be interpreted as an integer

# Fixed code
def _process_packet(self, pkt):
    """Process each packet: matches the TCP seq/ack numbers
    to follow the TCP streams, and orders the fragments.
    """
    if self.app:
        # Special mode: Application layer. Use on top of TCP
        pay_class = pkt.__class__
        if not hasattr(pay_class, "tcp_reassemble"):
            # Being on top of TCP, we have no way of knowing
            # when a packet ends.
            return pkt
        self.data += bytes(pkt)
        pkt = pay_class.tcp_reassemble(self.data, self.metadata)
        if pkt:
            self.data = b""
            self.metadata = {}
            return pkt
        return

    from scapy.layers.inet import IP, TCP

    if not pkt or TCP not in pkt:
        return pkt
    pay = pkt[TCP].payload
    if isinstance(pay, (NoPayload, conf.padding_layer)):
        return pkt
    new_data = pay.original
    # Match packets by a uniqute TCP identifier
    seq = pkt[TCP].seq
    ident = pkt.sprintf(self.fmt)
    data, metadata = self.tcp_frags[ident]
    # Let's guess which class is going to be used
    if "pay_class" not in metadata:
        pay_class = pay.__class__
        if hasattr(pay_class, "tcp_reassemble"):
            tcp_reassemble = pay_class.tcp_reassemble
        else:
            # We can't know for sure when a packet ends.
            # Ignore.
            return pkt
        metadata["pay_class"] = pay_class
        metadata["tcp_reassemble"] = tcp_reassemble
    else:
        tcp_reassemble = metadata["tcp_reassemble"]
    # Get a relative sequence number for a storage purpose
    relative_seq = metadata.get("relative_seq", None)
    if relative_seq is None:
        relative_seq = metadata["relative_seq"] = seq - 1
    seq = seq - relative_seq
    # Add the data to the buffer
    # Note that this take care of retransmission packets.
    data.append(new_data, seq)
    # Check TCP FIN or TCP RESET
    if pkt[TCP].flags.F or pkt[TCP].flags.R:
        metadata["tcp_end"] = True

    # In case any app layer protocol requires it,
    # allow the parser to inspect TCP PSH flag
    if pkt[TCP].flags.P:
        metadata["tcp_psh"] = True
    # XXX TODO: check that no empty space is missing in the buffer.
    # XXX Currently, if a TCP fragment was missing, we won't notice it.
    packet = None
    if data.full():
        # Reassemble using all previous packets
        packet = tcp_reassemble(bytes(data), metadata)
    # Stack the result on top of the previous frames
    if packet:
        data.clear()
        metadata.clear()
        del self.tcp_frags[ident]
        pay.underlayer.remove_payload()
        if IP in pkt:
            pkt[IP].len = None
            pkt[IP].chksum = None
        return pkt / packet