gh-146442: Fix various bugs in compiler pipeline (#146443)

Fix null derefs, missing decrefs, and unchecked returns from bug report.
This commit is contained in:
Brandon 2026-03-30 15:04:04 -05:00 committed by GitHub
parent e79fd60339
commit ca95e979d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 4 deletions

View file

@ -418,6 +418,7 @@ assemble_emit_instr(struct assembler *a, instruction *instr)
int size = instr_size(instr);
if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
if (len > PY_SSIZE_T_MAX / 2) {
PyErr_NoMemory();
return ERROR;
}
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_bytecode, len * 2));

View file

@ -667,8 +667,8 @@ codegen_unwind_fblock_stack(compiler *c, location *ploc,
_PyCompile_PopFBlock(c, top->fb_type, top->fb_block);
RETURN_IF_ERROR(codegen_unwind_fblock(c, ploc, &copy, preserve_tos));
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, ploc, preserve_tos, loop));
_PyCompile_PushFBlock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
copy.fb_exit, copy.fb_datum);
RETURN_IF_ERROR(_PyCompile_PushFBlock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
copy.fb_exit, copy.fb_datum));
return SUCCESS;
}
@ -715,10 +715,14 @@ codegen_setup_annotations_scope(compiler *c, location loc,
// if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError
PyObject *value_with_fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);
if (value_with_fake_globals == NULL) {
return ERROR;
}
assert(!SYMTABLE_ENTRY(c)->ste_has_docstring);
_Py_DECLARE_STR(format, ".format");
ADDOP_I(c, loc, LOAD_FAST, 0);
ADDOP_LOAD_CONST(c, loc, value_with_fake_globals);
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
NEW_JUMP_TARGET_LABEL(c, body);
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
@ -794,6 +798,9 @@ codegen_deferred_annotations_body(compiler *c, location loc,
if (!mangled) {
return ERROR;
}
// NOTE: ref of mangled can be leaked on ADDOP* and VISIT macros due to early returns
// fixing would require an overhaul of these macros
PyObject *cond_index = PyList_GET_ITEM(conditional_annotation_indices, i);
assert(PyLong_CheckExact(cond_index));
long idx = PyLong_AS_LONG(cond_index);
@ -3279,7 +3286,10 @@ codegen_nameop(compiler *c, location loc,
}
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
RETURN_IF_ERROR(scope);
if (scope == -1) {
goto error;
}
_PyCompile_optype optype;
Py_ssize_t arg = 0;
if (_PyCompile_ResolveNameop(c, mangled, scope, &optype, &arg) < 0) {

View file

@ -1100,18 +1100,22 @@ _PyCompile_TweakInlinedComprehensionScopes(compiler *c, location loc,
assert(orig == NULL || orig == Py_True || orig == Py_False);
if (orig != Py_True) {
if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
Py_XDECREF(orig);
return ERROR;
}
if (state->fast_hidden == NULL) {
state->fast_hidden = PySet_New(NULL);
if (state->fast_hidden == NULL) {
Py_XDECREF(orig);
return ERROR;
}
}
if (PySet_Add(state->fast_hidden, k) < 0) {
Py_XDECREF(orig);
return ERROR;
}
}
Py_XDECREF(orig);
}
}
}