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

Skip to content

Commit ce6ea31

Browse files
committed
gh-108308: Replace PyDict_GetItem() with PyDict_GetItemRef()
Replace PyDict_GetItem() calls with PyDict_GetItemRef() to handle errors. pycore_init_builtins() now checks for _PyType_Lookup() failure.
1 parent adfc118 commit ce6ea31

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

Python/assemble.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
466466
extern void _Py_set_localsplus_info(int, PyObject *, unsigned char,
467467
PyObject *, PyObject *);
468468

469-
static void
469+
static int
470470
compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
471471
PyObject *names, PyObject *kinds)
472472
{
@@ -481,7 +481,11 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
481481
if (PyDict_Contains(umd->u_fasthidden, k)) {
482482
kind |= CO_FAST_HIDDEN;
483483
}
484-
if (PyDict_GetItem(umd->u_cellvars, k) != NULL) {
484+
int has_cell = PyDict_Contains(umd->u_cellvars, k);
485+
if (has_cell < 0) {
486+
return -1;
487+
}
488+
if (has_cell) {
485489
kind |= CO_FAST_CELL;
486490
}
487491
_Py_set_localsplus_info(offset, k, kind, names, kinds);
@@ -492,7 +496,11 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
492496
int numdropped = 0;
493497
pos = 0;
494498
while (PyDict_Next(umd->u_cellvars, &pos, &k, &v)) {
495-
if (PyDict_GetItem(umd->u_varnames, k) != NULL) {
499+
int has_name = PyDict_Contains(umd->u_varnames, k);
500+
if (has_name < 0) {
501+
return -1;
502+
}
503+
if (has_name) {
496504
// Skip cells that are already covered by locals.
497505
numdropped += 1;
498506
continue;
@@ -512,6 +520,7 @@ compute_localsplus_info(_PyCompile_CodeUnitMetadata *umd, int nlocalsplus,
512520
assert(offset < nlocalsplus);
513521
_Py_set_localsplus_info(offset, k, CO_FAST_FREE, names, kinds);
514522
}
523+
return 0;
515524
}
516525

517526
static PyCodeObject *
@@ -556,7 +565,10 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
556565
if (localspluskinds == NULL) {
557566
goto error;
558567
}
559-
compute_localsplus_info(umd, nlocalsplus, localsplusnames, localspluskinds);
568+
if (compute_localsplus_info(umd, nlocalsplus,
569+
localsplusnames, localspluskinds) < 0) {
570+
goto error;
571+
}
560572

561573
struct _PyCodeConstructor con = {
562574
.filename = filename,

Python/compile.c

+20-4
Original file line numberDiff line numberDiff line change
@@ -4194,9 +4194,20 @@ compiler_nameop(struct compiler *c, location loc,
41944194
optype = OP_DEREF;
41954195
break;
41964196
case LOCAL:
4197-
if (_PyST_IsFunctionLike(c->u->u_ste) ||
4198-
(PyDict_GetItem(c->u->u_metadata.u_fasthidden, mangled) == Py_True))
4197+
if (_PyST_IsFunctionLike(c->u->u_ste)) {
41994198
optype = OP_FAST;
4199+
}
4200+
else {
4201+
PyObject *item;
4202+
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled,
4203+
&item) < 0) {
4204+
return ERROR;
4205+
}
4206+
if (item == Py_True) {
4207+
optype = OP_FAST;
4208+
}
4209+
Py_XDECREF(item);
4210+
}
42004211
break;
42014212
case GLOBAL_IMPLICIT:
42024213
if (_PyST_IsFunctionLike(c->u->u_ste))
@@ -5518,8 +5529,13 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
55185529
if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) || in_class_block) {
55195530
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
55205531
// non-function scope: override this name to use fast locals
5521-
PyObject *orig = PyDict_GetItem(c->u->u_metadata.u_fasthidden, k);
5522-
if (orig != Py_True) {
5532+
PyObject *orig;
5533+
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, k, &orig) < 0) {
5534+
return ERROR;
5535+
}
5536+
int orig_is_true = (orig == Py_True);
5537+
Py_XDECREF(orig);
5538+
if (!orig_is_true) {
55235539
if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
55245540
return ERROR;
55255541
}

Python/flowgraph.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -2404,12 +2404,16 @@ build_cellfixedoffsets(_PyCompile_CodeUnitMetadata *umd)
24042404
PyObject *varname, *cellindex;
24052405
Py_ssize_t pos = 0;
24062406
while (PyDict_Next(umd->u_cellvars, &pos, &varname, &cellindex)) {
2407-
PyObject *varindex = PyDict_GetItem(umd->u_varnames, varname);
2407+
PyObject *varindex;
2408+
if (PyDict_GetItemRef(umd->u_varnames, varname, &varindex) < 0) {
2409+
return NULL;
2410+
}
24082411
if (varindex != NULL) {
24092412
assert(PyLong_AS_LONG(cellindex) < INT_MAX);
24102413
assert(PyLong_AS_LONG(varindex) < INT_MAX);
24112414
int oldindex = (int)PyLong_AS_LONG(cellindex);
24122415
int argoffset = (int)PyLong_AS_LONG(varindex);
2416+
Py_DECREF(varindex);
24132417
fixed[oldindex] = argoffset;
24142418
}
24152419
}

Python/pylifecycle.c

+20-6
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,32 @@ pycore_init_builtins(PyThreadState *tstate)
762762
}
763763
interp->builtins = Py_NewRef(builtins_dict);
764764

765-
PyObject *isinstance = PyDict_GetItem(builtins_dict, &_Py_ID(isinstance));
766-
assert(isinstance);
765+
PyObject *isinstance;
766+
if (PyDict_GetItemRef(builtins_dict, &_Py_ID(isinstance), &isinstance) != 1) {
767+
goto error;
768+
}
767769
interp->callable_cache.isinstance = isinstance;
768-
PyObject *len = PyDict_GetItem(builtins_dict, &_Py_ID(len));
769-
assert(len);
770+
Py_DECREF(isinstance);
771+
772+
PyObject *len;
773+
if (PyDict_GetItemRef(builtins_dict, &_Py_ID(len), &len) != 1) {
774+
goto error;
775+
}
770776
interp->callable_cache.len = len;
777+
Py_DECREF(len);
778+
771779
PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append));
772-
assert(list_append);
780+
if (list_append == NULL) {
781+
goto error;
782+
}
773783
interp->callable_cache.list_append = list_append;
784+
774785
PyObject *object__getattribute__ = _PyType_Lookup(&PyBaseObject_Type, &_Py_ID(__getattribute__));
775-
assert(object__getattribute__);
786+
if (object__getattribute__ == NULL) {
787+
goto error;
788+
}
776789
interp->callable_cache.object__getattribute__ = object__getattribute__;
790+
777791
if (_PyBuiltins_AddExceptions(bimod) < 0) {
778792
return _PyStatus_ERR("failed to add exceptions to builtins");
779793
}

0 commit comments

Comments
 (0)