Skip to content

Commit 74d5c9d

Browse files
[3.11] pythongh-105375: Harden _ssl initialisation (python#105599)
(cherry picked from commit 01f4230) Add proper error handling to prevent reference leaks and overwritten exceptions. Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 3c08e54 commit 74d5c9d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bugs in :mod:`!_ssl` initialisation which could lead to leaked
2+
references and overwritten exceptions.

Modules/_ssl.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6052,17 +6052,26 @@ sslmodule_init_errorcodes(PyObject *module)
60526052

60536053
errcode = error_codes;
60546054
while (errcode->mnemonic != NULL) {
6055-
PyObject *mnemo, *key;
6056-
mnemo = PyUnicode_FromString(errcode->mnemonic);
6057-
key = Py_BuildValue("ii", errcode->library, errcode->reason);
6058-
if (mnemo == NULL || key == NULL)
6055+
PyObject *mnemo = PyUnicode_FromString(errcode->mnemonic);
6056+
if (mnemo == NULL) {
60596057
return -1;
6060-
if (PyDict_SetItem(state->err_codes_to_names, key, mnemo))
6058+
}
6059+
PyObject *key = Py_BuildValue("ii", errcode->library, errcode->reason);
6060+
if (key == NULL) {
6061+
Py_DECREF(mnemo);
60616062
return -1;
6062-
if (PyDict_SetItem(state->err_names_to_codes, mnemo, key))
6063+
}
6064+
if (PyDict_SetItem(state->err_codes_to_names, key, mnemo) < 0) {
6065+
Py_DECREF(key);
6066+
Py_DECREF(mnemo);
60636067
return -1;
6068+
}
6069+
int rc = PyDict_SetItem(state->err_codes_to_names, key, mnemo);
60646070
Py_DECREF(key);
60656071
Py_DECREF(mnemo);
6072+
if (rc < 0) {
6073+
return -1;
6074+
}
60666075
errcode++;
60676076
}
60686077

0 commit comments

Comments
 (0)