[New-bugs-announce] [issue25828] PyCode_Optimize() (peephole optimizer) doesn't handle KeyboardInterrupt correctly

STINNER Victor report at bugs.python.org
Wed Dec 9 03:03:24 EST 2015


New submission from STINNER Victor:

The peephole optimizer computes 2**(2**100), but if I press CTRL+c (the result will probably kills my memory anyway), I get an assertion error (with a Python compiled in debug mode).

$ ./python
>>> 2**(2**100)
^C
python: Python/ceval.c:1218: PyEval_EvalFrameEx: Assertion `!PyErr_Occurred()' failed.
Abandon (core dumped)

fold_binops_on_constants() returns 0 with an exception (KeyboardInterrupt) raised. The problem is in the caller which doesn't handle the exception properly:

                if (h >= 0 &&
                    ISBASICBLOCK(blocks, h, i-h+1)  &&
                    fold_binops_on_constants(&codestr[i], consts, CONST_STACK_LASTN(2))) {
                    i -= 2;
                    memset(&codestr[h], NOP, i - h);
                    assert(codestr[i] == LOAD_CONST);
                    CONST_STACK_POP(2);
                    CONST_STACK_PUSH_OP(i);
                }

There is probably the same error on fold_unaryops_on_constants().


Python 2.7 looks to behave correctly:

$ python2.7
>>> 2**(2**100)
^C
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt


But in fact Python 2.7 is much worse :-D Peephole optimizer of Python 2.7 clears *all* exceptions (!) and it only optimizes 2**100, but not 2**(2**100). That's why the bug is not easily reproduced on Python 2.7. fold_binops_on_constants():

    if (newconst == NULL) {
        if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
            PyErr_Clear();
        return 0;
    }

----------
messages: 256143
nosy: haypo
priority: normal
severity: normal
status: open
title: PyCode_Optimize() (peephole optimizer) doesn't handle KeyboardInterrupt correctly
type: crash
versions: Python 2.7, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25828>
_______________________________________


More information about the New-bugs-announce mailing list