aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/flowgraph.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-08-23 17:40:26 +0200
committerGitHub <noreply@github.com>2023-08-23 17:40:26 +0200
commitf5559f38d9831e7e55a518e516bcd620ec13af14 (patch)
treeb605c775e1bc02b864d17e663400cde01562088d /Python/flowgraph.c
parent154477be722ae5c4e18d22d0860e284006b09c4f (diff)
downloadcpython-f5559f38d9831e7e55a518e516bcd620ec13af14.tar.gz
cpython-f5559f38d9831e7e55a518e516bcd620ec13af14.zip
gh-108308: Replace PyDict_GetItem() with PyDict_GetItemRef() (#108309)
Replace PyDict_GetItem() calls with PyDict_GetItemRef() or PyDict_GetItemWithError() to handle errors. * Replace PyLong_AS_LONG() with _PyLong_AsInt() and check for errors. * Check for PyDict_Contains() error. * pycore_init_builtins() checks for _PyType_Lookup() failure.
Diffstat (limited to 'Python/flowgraph.c')
-rw-r--r--Python/flowgraph.c30
1 files changed, 22 insertions, 8 deletions
diff --git a/Python/flowgraph.c b/Python/flowgraph.c
index 719ed921050..e620e7cf1b9 100644
--- a/Python/flowgraph.c
+++ b/Python/flowgraph.c
@@ -2404,17 +2404,31 @@ build_cellfixedoffsets(_PyCompile_CodeUnitMetadata *umd)
PyObject *varname, *cellindex;
Py_ssize_t pos = 0;
while (PyDict_Next(umd->u_cellvars, &pos, &varname, &cellindex)) {
- PyObject *varindex = PyDict_GetItem(umd->u_varnames, varname);
- if (varindex != NULL) {
- assert(PyLong_AS_LONG(cellindex) < INT_MAX);
- assert(PyLong_AS_LONG(varindex) < INT_MAX);
- int oldindex = (int)PyLong_AS_LONG(cellindex);
- int argoffset = (int)PyLong_AS_LONG(varindex);
- fixed[oldindex] = argoffset;
+ PyObject *varindex;
+ if (PyDict_GetItemRef(umd->u_varnames, varname, &varindex) < 0) {
+ goto error;
+ }
+ if (varindex == NULL) {
+ continue;
+ }
+
+ int argoffset = _PyLong_AsInt(varindex);
+ Py_DECREF(varindex);
+ if (argoffset == -1 && PyErr_Occurred()) {
+ goto error;
}
- }
+ int oldindex = _PyLong_AsInt(cellindex);
+ if (oldindex == -1 && PyErr_Occurred()) {
+ goto error;
+ }
+ fixed[oldindex] = argoffset;
+ }
return fixed;
+
+error:
+ PyMem_Free(fixed);
+ return NULL;
}
#define IS_GENERATOR(CF) \