bpo-35454: Fix miscellaneous minor issues in error handling. (GH-11077)
* bpo-35454: Fix miscellaneous minor issues in error handling.
* Fix a null pointer dereference.
(cherry picked from commit 8905fcc85a6fc3ac394bc89b0bbf40897e9497a6)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 3118b55..ff3a240 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -352,7 +352,10 @@
return NULL;
}
attrib = PyDict_Copy(attrib);
- PyDict_DelItem(kwds, attrib_str);
+ if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
+ Py_DECREF(attrib);
+ attrib = NULL;
+ }
} else {
attrib = PyDict_New();
}
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 16d3191..2a6b893 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -777,9 +777,11 @@
for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
tstate;
tstate = PyThreadState_Next(tstate))
- if (tstate->dict &&
- PyDict_GetItem(tstate->dict, self->key))
- PyDict_DelItem(tstate->dict, self->key);
+ if (tstate->dict && PyDict_GetItem(tstate->dict, self->key)) {
+ if (PyDict_DelItem(tstate->dict, self->key)) {
+ PyErr_Clear();
+ }
+ }
}
return 0;
}
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 53bf996..e6d3f8b 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -360,10 +360,14 @@
else {
if (PyDict_GetItemString(
dict,
- win_runtime_flags[i].flag_name) != NULL) {
- PyDict_DelItemString(
- dict,
- win_runtime_flags[i].flag_name);
+ win_runtime_flags[i].flag_name) != NULL)
+ {
+ if (PyDict_DelItemString(
+ dict,
+ win_runtime_flags[i].flag_name))
+ {
+ PyErr_Clear();
+ }
}
}
}
diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c
index 7de102e..c2f9d30 100644
--- a/Objects/namespaceobject.c
+++ b/Objects/namespaceobject.c
@@ -103,15 +103,15 @@
PyObject *value, *item;
value = PyDict_GetItem(d, key);
- assert(value != NULL);
-
- item = PyUnicode_FromFormat("%S=%R", key, value);
- if (item == NULL) {
- loop_error = 1;
- }
- else {
- loop_error = PyList_Append(pairs, item);
- Py_DECREF(item);
+ if (value != NULL) {
+ item = PyUnicode_FromFormat("%S=%R", key, value);
+ if (item == NULL) {
+ loop_error = 1;
+ }
+ else {
+ loop_error = PyList_Append(pairs, item);
+ Py_DECREF(item);
+ }
}
}
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 29e475d..80dca65 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -256,7 +256,11 @@
version_obj = _PyDict_GetItemId(registry, &PyId_version);
if (version_obj == NULL
|| !PyLong_CheckExact(version_obj)
- || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
+ || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version)
+ {
+ if (PyErr_Occurred()) {
+ return -1;
+ }
PyDict_Clear(registry);
version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
if (version_obj == NULL)
diff --git a/Python/ceval.c b/Python/ceval.c
index e371d34..00b2e6d 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5027,7 +5027,7 @@
PyObject *names = f->f_code->co_names;
PyObject *name = GETITEM(names, oparg);
PyObject *locals = f->f_locals;
- if (PyDict_CheckExact(locals) &&
+ if (locals && PyDict_CheckExact(locals) &&
PyDict_GetItem(locals, name) == v) {
if (PyDict_DelItem(locals, name) != 0) {
PyErr_Clear();
diff --git a/Python/codecs.c b/Python/codecs.c
index eb3cd35..c315ae3 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -130,8 +130,10 @@
/* Next, scan the search functions in order of registration */
args = PyTuple_New(1);
- if (args == NULL)
- goto onError;
+ if (args == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
PyTuple_SET_ITEM(args,0,v);
len = PyList_Size(interp->codec_search_path);
diff --git a/Python/import.c b/Python/import.c
index 709fc43..5f5d135 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2138,10 +2138,10 @@
}
mod = _PyImport_FindExtensionObject(name, path);
- if (mod != NULL) {
+ if (mod != NULL || PyErr_Occurred()) {
Py_DECREF(name);
Py_DECREF(path);
- Py_INCREF(mod);
+ Py_XINCREF(mod);
return mod;
}
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 4b08c9c..94b6d43 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1417,6 +1417,9 @@
PyDict_SetItemString(interp->sysdict, "modules", modules);
_PySys_EndInit(interp->sysdict, &interp->config);
}
+ else if (PyErr_Occurred()) {
+ goto handle_error;
+ }
bimod = _PyImport_FindBuiltin("builtins", modules);
if (bimod != NULL) {
@@ -1425,6 +1428,9 @@
goto handle_error;
Py_INCREF(interp->builtins);
}
+ else if (PyErr_Occurred()) {
+ goto handle_error;
+ }
/* initialize builtin exceptions */
_PyExc_Init(bimod);