Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-115376: fix segfault in _testinternalcapi.compiler_codegen on bad input #115379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/test/test_compiler_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class IsolatedCodeGenTests(CodegenTestCase):

def codegen_test(self, snippet, expected_insts):
import ast
a = ast.parse(snippet, "my_file.py", "exec");
a = ast.parse(snippet, "my_file.py", "exec")
insts = self.generate_code(a)
self.assertInstructionsMatch(insts, expected_insts)

Expand Down Expand Up @@ -54,3 +54,8 @@ def test_for_loop(self):
('RETURN_VALUE', None),
]
self.codegen_test(snippet, expected)

def test_syntax_error__return_not_in_function(self):
snippet = "return 42"
with self.assertRaisesRegex(SyntaxError, "'return' outside function"):
self.codegen_test(snippet, None)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix segfault in ``_testinternalcapi.compiler_codegen`` on bad input.
42 changes: 28 additions & 14 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1737,16 +1737,10 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
static int
compiler_codegen(struct compiler *c, mod_ty mod)
{
_Py_DECLARE_STR(anon_module, "<module>");
RETURN_IF_ERROR(
compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
mod, 1));

location loc = LOCATION(1, 1, 0, 0);
switch (mod->kind) {
case Module_kind:
if (compiler_body(c, loc, mod->v.Module.body) < 0) {
compiler_exit_scope(c);
return ERROR;
}
break;
Expand All @@ -1755,10 +1749,10 @@ compiler_codegen(struct compiler *c, mod_ty mod)
ADDOP(c, loc, SETUP_ANNOTATIONS);
}
c->c_interactive = 1;
VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
VISIT_SEQ(c, stmt, mod->v.Interactive.body);
break;
case Expression_kind:
VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
VISIT(c, expr, mod->v.Expression.body);
break;
default:
PyErr_Format(PyExc_SystemError,
Expand All @@ -1769,14 +1763,29 @@ compiler_codegen(struct compiler *c, mod_ty mod)
return SUCCESS;
}

static int
compiler_enter_anonymous_scope(struct compiler* c, mod_ty mod)
{
_Py_DECLARE_STR(anon_module, "<module>");
RETURN_IF_ERROR(
compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
mod, 1));
return SUCCESS;
}

static PyCodeObject *
compiler_mod(struct compiler *c, mod_ty mod)
{
PyCodeObject *co = NULL;
int addNone = mod->kind != Expression_kind;
if (compiler_codegen(c, mod) < 0) {
if (compiler_enter_anonymous_scope(c, mod) < 0) {
return NULL;
}
PyCodeObject *co = optimize_and_assemble(c, addNone);
if (compiler_codegen(c, mod) < 0) {
goto finally;
}
co = optimize_and_assemble(c, addNone);
finally:
compiler_exit_scope(c);
return co;
}
Expand Down Expand Up @@ -7918,15 +7927,20 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
return NULL;
}

metadata = PyDict_New();
if (metadata == NULL) {
return NULL;
}

if (compiler_enter_anonymous_scope(c, mod) < 0) {
return NULL;
}
if (compiler_codegen(c, mod) < 0) {
goto finally;
}

_PyCompile_CodeUnitMetadata *umd = &c->u->u_metadata;
metadata = PyDict_New();
if (metadata == NULL) {
goto finally;
}

#define SET_MATADATA_ITEM(key, value) \
if (value != NULL) { \
if (PyDict_SetItemString(metadata, key, value) < 0) goto finally; \
Expand Down