Skip to content

Commit 089c19b

Browse files
committed
fix null derefs, missing decrefs, and unchecked returns from bug report
1 parent 0055140 commit 089c19b

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
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: 13 additions & 5 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); // macro handles decref of 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);
@@ -816,7 +820,7 @@ codegen_deferred_annotations_body(compiler *c, location loc,
816820

817821
VISIT(c, expr, st->v.AnnAssign.annotation);
818822
ADDOP_I(c, LOC(st), COPY, 2);
819-
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled);
823+
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled); // macro handles decref of mangle
820824
// stack now contains <annos> <name> <annos> <value>
821825
ADDOP(c, loc, STORE_SUBSCR);
822826
// stack now contains <annos>
@@ -3279,7 +3283,11 @@ codegen_nameop(compiler *c, location loc,
32793283
}
32803284

32813285
int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
3282-
RETURN_IF_ERROR(scope);
3286+
if (scope == -1) {
3287+
Py_DECREF(mangled);
3288+
return ERROR;
3289+
}
3290+
32833291
_PyCompile_optype optype;
32843292
Py_ssize_t arg = 0;
32853293
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)