Skip to content

Commit 6a032b0

Browse files
committed
gh->108308: Replace PyDict_GetItem() with PyDict_GetItemRef()
Replace PyDict_GetItem() calls with PyDict_GetItemRef() to handle errors.
1 parent adfc118 commit 6a032b0

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

Python/assemble.c

Lines changed: 16 additions & 4 deletions
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+
PyObject *cell;
485+
if (PyDict_GetItemRef(umd->u_cellvars, k, &cell) < 0) {
486+
return -1;
487+
}
488+
if (cell != NULL) {
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+
PyObject *name;
500+
if (PyDict_GetItemRef(umd->u_varnames, k, &name) < 0) {
501+
return -1;
502+
}
503+
if (name != NULL) {
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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,9 +4194,19 @@ 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+
}
42004210
break;
42014211
case GLOBAL_IMPLICIT:
42024212
if (_PyST_IsFunctionLike(c->u->u_ste))
@@ -5518,7 +5528,10 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
55185528
if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) || in_class_block) {
55195529
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
55205530
// non-function scope: override this name to use fast locals
5521-
PyObject *orig = PyDict_GetItem(c->u->u_metadata.u_fasthidden, k);
5531+
PyObject *orig;
5532+
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, k, &orig) < 0) {
5533+
return ERROR;
5534+
}
55225535
if (orig != Py_True) {
55235536
if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
55245537
return ERROR;

Python/flowgraph.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,10 @@ 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);

Python/pylifecycle.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,11 +762,15 @@ 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+
PyObject *len;
771+
if (PyDict_GetItemRef(builtins_dict, &_Py_ID(len), &len) != 1) {
772+
goto error;
773+
}
770774
interp->callable_cache.len = len;
771775
PyObject *list_append = _PyType_Lookup(&PyList_Type, &_Py_ID(append));
772776
assert(list_append);

0 commit comments

Comments
 (0)