Skip to content

Commit 01f4230

Browse files
gh-105375: Harden _ssl initialisation (#105599)
Add proper error handling to prevent reference leaks and overwritten exceptions.
1 parent cc87948 commit 01f4230

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6001,15 +6001,21 @@ sslmodule_init_errorcodes(PyObject *module)
60016001

60026002
errcode = error_codes;
60036003
while (errcode->mnemonic != NULL) {
6004-
PyObject *mnemo, *key;
6005-
mnemo = PyUnicode_FromString(errcode->mnemonic);
6006-
key = Py_BuildValue("ii", errcode->library, errcode->reason);
6007-
if (mnemo == NULL || key == NULL)
6004+
PyObject *mnemo = PyUnicode_FromString(errcode->mnemonic);
6005+
if (mnemo == NULL) {
60086006
return -1;
6009-
if (PyDict_SetItem(state->err_codes_to_names, key, mnemo))
6007+
}
6008+
PyObject *key = Py_BuildValue("ii", errcode->library, errcode->reason);
6009+
if (key == NULL) {
6010+
Py_DECREF(mnemo);
60106011
return -1;
6012+
}
6013+
int rc = PyDict_SetItem(state->err_codes_to_names, key, mnemo);
60116014
Py_DECREF(key);
60126015
Py_DECREF(mnemo);
6016+
if (rc < 0) {
6017+
return -1;
6018+
}
60136019
errcode++;
60146020
}
60156021

0 commit comments

Comments
 (0)