Skip to content

Commit ca95e97

Browse files
authored
gh-146442: Fix various bugs in compiler pipeline (#146443)
Fix null derefs, missing decrefs, and unchecked returns from bug report.
1 parent e79fd60 commit ca95e97

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Python/assemble.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ assemble_emit_instr(struct assembler *a, instruction *instr)
418418
int size = instr_size(instr);
419419
if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
420420
if (len > PY_SSIZE_T_MAX / 2) {
421+
PyErr_NoMemory();
421422
return ERROR;
422423
}
423424
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_bytecode, len * 2));

Python/codegen.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,8 @@ codegen_unwind_fblock_stack(compiler *c, location *ploc,
667667
_PyCompile_PopFBlock(c, top->fb_type, top->fb_block);
668668
RETURN_IF_ERROR(codegen_unwind_fblock(c, ploc, &copy, preserve_tos));
669669
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, ploc, preserve_tos, loop));
670-
_PyCompile_PushFBlock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
671-
copy.fb_exit, copy.fb_datum);
670+
RETURN_IF_ERROR(_PyCompile_PushFBlock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
671+
copy.fb_exit, copy.fb_datum));
672672
return SUCCESS;
673673
}
674674

@@ -715,10 +715,14 @@ codegen_setup_annotations_scope(compiler *c, location loc,
715715

716716
// if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError
717717
PyObject *value_with_fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);
718+
if (value_with_fake_globals == NULL) {
719+
return ERROR;
720+
}
721+
718722
assert(!SYMTABLE_ENTRY(c)->ste_has_docstring);
719723
_Py_DECLARE_STR(format, ".format");
720724
ADDOP_I(c, loc, LOAD_FAST, 0);
721-
ADDOP_LOAD_CONST(c, loc, value_with_fake_globals);
725+
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);
722726
ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
723727
NEW_JUMP_TARGET_LABEL(c, body);
724728
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
@@ -794,6 +798,9 @@ codegen_deferred_annotations_body(compiler *c, location loc,
794798
if (!mangled) {
795799
return ERROR;
796800
}
801+
// NOTE: ref of mangled can be leaked on ADDOP* and VISIT macros due to early returns
802+
// fixing would require an overhaul of these macros
803+
797804
PyObject *cond_index = PyList_GET_ITEM(conditional_annotation_indices, i);
798805
assert(PyLong_CheckExact(cond_index));
799806
long idx = PyLong_AS_LONG(cond_index);
@@ -3279,7 +3286,10 @@ codegen_nameop(compiler *c, location loc,
32793286
}
32803287

32813288
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
3282-
RETURN_IF_ERROR(scope);
3289+
if (scope == -1) {
3290+
goto error;
3291+
}
3292+
32833293
_PyCompile_optype optype;
32843294
Py_ssize_t arg = 0;
32853295
if (_PyCompile_ResolveNameop(c, mangled, scope, &optype, &arg) < 0) {

Python/compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,18 +1100,22 @@ _PyCompile_TweakInlinedComprehensionScopes(compiler *c, location loc,
11001100
assert(orig == NULL || orig == Py_True || orig == Py_False);
11011101
if (orig != Py_True) {
11021102
if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
1103+
Py_XDECREF(orig);
11031104
return ERROR;
11041105
}
11051106
if (state->fast_hidden == NULL) {
11061107
state->fast_hidden = PySet_New(NULL);
11071108
if (state->fast_hidden == NULL) {
1109+
Py_XDECREF(orig);
11081110
return ERROR;
11091111
}
11101112
}
11111113
if (PySet_Add(state->fast_hidden, k) < 0) {
1114+
Py_XDECREF(orig);
11121115
return ERROR;
11131116
}
11141117
}
1118+
Py_XDECREF(orig);
11151119
}
11161120
}
11171121
}

0 commit comments

Comments
 (0)