# Buggy code
def _check_len(self, pkt):
    """Check for odd packet length and pad according to Cisco spec.
    This padding is only used for checksum computation.  The original
    packet should not be altered."""
    if len(pkt) % 2:
        last_chr = pkt[-1]
        if last_chr <= b"\x80":
            return pkt[:-1] + b"\x00" + last_chr
        else:
            return pkt[:-1] + b"\xff" + chb(orb(last_chr) - 1)
    else:
        return pkt


# Traceback (TypeError)
root@ubuntu:/workspace/_prototype/rtg-avatar# python
Python 3.7.5 (default, Nov  7 2019, 10:50:52)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
from scapy.all import *
from scapy.contrib import *
pkt=CDPv2_HDR(vers=2, ttl=180, msg='123')
len(pkt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 522, in __len__
return len(self.__bytes__())
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 487, in __bytes__
return self.build()
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 607, in build
p = self.do_build()
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 594, in do_build
return self.post_build(pkt, pay)
File "/usr/local/lib/python3.7/dist-packages/scapy/contrib/cdp.py", line 371, in post_build
cksum = checksum(self._check_len(p))
File "/usr/local/lib/python3.7/dist-packages/scapy/contrib/cdp.py", line 361, in _check_len
if last_chr <= b'\x80':
TypeError: '<=' not supported between instances of 'int' and 'bytes'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 522, in __len__
return len(self.__bytes__())
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 487, in __bytes__
return self.build()
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 607, in build
p = self.do_build()
File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 594, in do_build
return self.post_build(pkt, pay)
File "/usr/local/lib/python3.7/dist-packages/scapy/contrib/cdp.py", line 371, in post_build
cksum = checksum(self._check_len(p))
File "/usr/local/lib/python3.7/dist-packages/scapy/contrib/cdp.py", line 361, in _check_len
if last_chr <= b'\x80':
TypeError: '<=' not supported between instances of 'int' and 'bytes'


# Fixed code
def _check_len(self, pkt):
    """Check for odd packet length and pad according to Cisco spec.
    This padding is only used for checksum computation.  The original
    packet should not be altered."""
    if len(pkt) % 2:
        last_chr = orb(pkt[-1])
        if last_chr <= 0x80:
            return pkt[:-1] + b"\x00" + chb(last_chr)
        else:
            return pkt[:-1] + b"\xff" + chb(orb(last_chr) - 1)
    else:
        return pkt