mirror of
https://github.com/python/cpython.git
synced 2025-07-24 19:54:21 +00:00
(cherry picked from commit 20a56d8bec
)
Add proper error handling to add_errors_module() to prevent exceptions
from possibly being overwritten.
This commit is contained in:
parent
f98d475ee3
commit
e5fe017143
2 changed files with 15 additions and 10 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
Harden :mod:`pyexpat` error handling during module initialisation to prevent
|
||||||
|
exceptions from possibly being overwritten, and objects from being
|
||||||
|
dereferenced twice.
|
|
@ -1768,14 +1768,18 @@ add_error(PyObject *errors_module, PyObject *codes_dict,
|
||||||
static int
|
static int
|
||||||
add_errors_module(PyObject *mod)
|
add_errors_module(PyObject *mod)
|
||||||
{
|
{
|
||||||
|
// add_submodule() returns a borrowed ref.
|
||||||
PyObject *errors_module = add_submodule(mod, MODULE_NAME ".errors");
|
PyObject *errors_module = add_submodule(mod, MODULE_NAME ".errors");
|
||||||
if (errors_module == NULL) {
|
if (errors_module == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *codes_dict = PyDict_New();
|
PyObject *codes_dict = PyDict_New();
|
||||||
|
if (codes_dict == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
PyObject *rev_codes_dict = PyDict_New();
|
PyObject *rev_codes_dict = PyDict_New();
|
||||||
if (codes_dict == NULL || rev_codes_dict == NULL) {
|
if (rev_codes_dict == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1796,19 +1800,17 @@ add_errors_module(PyObject *mod)
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_INCREF(codes_dict);
|
int rc = PyModule_AddObjectRef(errors_module, "codes", codes_dict);
|
||||||
if (PyModule_AddObject(errors_module, "codes", codes_dict) < 0) {
|
|
||||||
Py_DECREF(codes_dict);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
Py_CLEAR(codes_dict);
|
Py_CLEAR(codes_dict);
|
||||||
|
if (rc < 0) {
|
||||||
Py_INCREF(rev_codes_dict);
|
|
||||||
if (PyModule_AddObject(errors_module, "messages", rev_codes_dict) < 0) {
|
|
||||||
Py_DECREF(rev_codes_dict);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rc = PyModule_AddObjectRef(errors_module, "messages", rev_codes_dict);
|
||||||
Py_CLEAR(rev_codes_dict);
|
Py_CLEAR(rev_codes_dict);
|
||||||
|
if (rc < 0) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue