[issue11105] Compiling evil ast crashes interpreter

Gregory P. Smith report at bugs.python.org
Fri Mar 16 01:33:04 CET 2012


Gregory P. Smith <greg at krypto.org> added the comment:

i haven't confirmed if it is this exact bug but I believe a coworker just ran into something similar.  he wrote code to use the ast to remove docstrings from code before passing it to compile() (as that saves a noticable amount of memory).  in the case the ast for code like:

def foo():
  """this is a docstring."""

Removing the docstring and passing such a thing to compile triggers a problem.  A workaround was to add a pass in such cases:

if (node.body and isinstance(node.body[0], ast.Expr) and
    isinstance(node.body[0].value, ast.Str)):
  docstring = node.body.pop(0)
  if len(node.body) == 0:
    # An empty body will sometimes provoke a segfault when you call
    # compile on the code object.
    node.body.append(ast.Pass(lineno=docstring.lineno,
                              col_offset=docstring.col_offset))

regardless, it'd be better if compile() *never* crashed on strange input.

----------
nosy: +gregory.p.smith

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


More information about the Python-bugs-list mailing list