Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105375: Harden pyexpat initialisation #105606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 11 additions & 7 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1775,14 +1775,18 @@ add_error(PyObject *errors_module, PyObject *codes_dict,
static int
add_errors_module(PyObject *mod)
{
// add_submodule() returns a borrowed ref.
PyObject *errors_module = add_submodule(mod, MODULE_NAME ".errors");
if (errors_module == NULL) {
return -1;
}

PyObject *codes_dict = PyDict_New();
if (codes_dict == NULL) {
return -1;
}
PyObject *rev_codes_dict = PyDict_New();
if (codes_dict == NULL || rev_codes_dict == NULL) {
if (rev_codes_dict == NULL) {
goto error;
}

Expand All @@ -1803,17 +1807,17 @@ add_errors_module(PyObject *mod)
goto error;
}

if (PyModule_AddObject(errors_module, "codes", Py_NewRef(codes_dict)) < 0) {
Py_DECREF(codes_dict);
int rc = PyModule_AddObjectRef(errors_module, "codes", codes_dict);
Py_CLEAR(codes_dict);
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
if (rc < 0) {
goto error;
}
Py_CLEAR(codes_dict);

if (PyModule_AddObject(errors_module, "messages", Py_NewRef(rev_codes_dict)) < 0) {
Py_DECREF(rev_codes_dict);
rc = PyModule_AddObjectRef(errors_module, "messages", rev_codes_dict);
Py_CLEAR(rev_codes_dict);
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
if (rc < 0) {
goto error;
}
Py_CLEAR(rev_codes_dict);

return 0;

Expand Down