Skip to content

Allocation failure while decoding a string literal's escapes returns NULL without an exception set #158309

Description

@lpyu001

Crash report

What happened?

Summary

When the buffer allocation in decode_unicode_with_escapes() fails while compiling a string literal that contains a backslash escape, the function returns NULL without setting an exception. In a debug build this aborts on assert(PyErr_Occurred()) in _Pypegen_raise_decode_error(). In a release build, compile() raises SyntaxError: invalid syntax for valid source instead of MemoryError.

Reproduction Code

import ast, _testcapi

for n in range(1, 200):
    _testcapi.set_nomemory(n, n + 1)
    try:
        compile("x = '\\n'", "<string>", "exec", ast.PyCF_ONLY_AST)
    except MemoryError:
        pass
    finally:
        _testcapi.remove_mem_hooks()
print("done")

Actual Behavior

Debug build (--with-pydebug):

python: Parser/pegen_errors.c:87: _Pypegen_raise_decode_error: Assertion `PyErr_Occurred()' failed.
Aborted (core dumped)

Release build:

Traceback (most recent call last):
  File "nomem_repro.py", line 6, in <module>
    compile("x = '\\n'", "<string>", "exec", ast.PyCF_ONLY_AST)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1
    x = '\n'
        ^^^^
SyntaxError: invalid syntax

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

No response

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parsertype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions