Skip to content

gh-156466: fix cleanup on error after Enter in codegen_enter_scope - #156467

Open
iritkatriel wants to merge 1 commit into
python:mainfrom
iritkatriel:gh-156466-1
Open

gh-156466: fix cleanup on error after Enter in codegen_enter_scope#156467
iritkatriel wants to merge 1 commit into
python:mainfrom
iritkatriel:gh-156466-1

Conversation

@iritkatriel

@iritkatriel iritkatriel commented Aug 27, 2026

Copy link
Copy Markdown
Member

If codegen_enter_scope errors after _PyCompile_EnterScope, it should exit the scope before returning ERROR.

@iritkatriel iritkatriel added 3.14 bugs and security fixes 3.15 pre-release feature fixes, bugs and security fixes 3.16 new features, bugs and security fixes skip news labels Aug 27, 2026
@picnixz picnixz added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed 3.14 bugs and security fixes 3.15 pre-release feature fixes, bugs and security fixes 3.16 new features, bugs and security fixes labels Aug 27, 2026
Comment thread Python/codegen.c
}

ADDOP_I(c, loc, RESUME, RESUME_AT_FUNC_START);
ADDOP_I_IN_SCOPE(c, loc, RESUME, RESUME_AT_FUNC_START);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also guard the final _PyCompile_ExitScope(c) in _PyCompile_CodeGen() with if (c->u != NULL), as compiler_mod() does?

If allocating the module's initial RESUME fails, this now exits the only scope and leaves c->u as NULL. _PyCompile_CodeGen() then reaches its unconditional final exit (Python/compile.c:1738), which dereferences c->u and segfaults.

I reproduced this with an allocator hook that fails the initial instruction-buffer allocation during:

import ast
import _testinternalcapi

_testinternalcapi.compiler_codegen(ast.parse("pass"), "<test>", 0)

Main propagates MemoryError, whereas this PR crashes. Normal compile() already handles the cleanup correctly.

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

Labels

awaiting merge needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants