MatthieuDartiailh / bytecode

Python module to modify bytecode
https://bytecode.readthedocs.io/
MIT License
302 stars 38 forks source link

`_encode_varint` cannot handle 0 #130

Closed P403n1x87 closed 1 year ago

P403n1x87 commented 1 year ago

The following exception is thrown when a TryBegin is the very first entry in a Bytecode object

value = 0, set_begin_marker = True

    @staticmethod
    def _encode_varint(value: int, set_begin_marker: bool = False) -> Iterator[int]:
        # Encode value as a varint on 7 bits (MSB should come first) and set
        # the begin marker if requested.
        temp: List[int] = []
        assert value >= 0
        while value:
            temp.append(value & 63 | (64 if temp else 0))
            value >>= 6
        if set_begin_marker:
>           temp[-1] |= 128
E           IndexError: list index out of range

It looks like the _encode_varint helper is failing to handle a value of 0 in this case. In Python 3.11 it is likely that the first opcode is e.g. RESUME, so in real Python code this issue might not occur.

MatthieuDartiailh commented 1 year ago

Closed by #132