[New-bugs-announce] [issue38115] Invalid bytecode offsets in co_lnotab

Thomas Wouters report at bugs.python.org
Wed Sep 11 10:27:50 EDT 2019

New submission from Thomas Wouters <thomas at python.org>:

The peephole optimizer in Python 2.7 and later (and probably a *lot* earlier) has a bug where if the optimizer entirely optimizes away the last line(s) of a function, the lnotab references invalid bytecode offsets:

>>> def f(cond1, cond2):
...     while 1:
...         return 3
...     while 1:
...         return 5
...     return 6
>>> list(dis.findlinestarts(f.__code__))
[(0, 3), (4, 5), (8, 6)]
>>> len(f.__code__.co_code)
>>> f.__code__.co_code[8]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index out of range

The problem is that the lnotab-readjustment in Python/peephole.c doesn't account for trailing NOPs in a bytecode string. I haven't been able to reproduce this before Python 3.8, probably because the optimizer wasn't capable of optimizing things aggressively enough to end a bytecode string with NOPs.

I have a fix for this bug already.

assignee: twouters
components: Interpreter Core
keywords: patch
messages: 351902
nosy: lukasz.langa, pablogsal, twouters
priority: release blocker
severity: normal
status: open
title: Invalid bytecode offsets in co_lnotab
type: behavior
versions: Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list