Skip to content

Commit eda06c8

Browse files
committed
fix memory leak in _get_crl_dp (closes #25569)
Patch started by Stéphane Wirtel.
1 parent 71a0b43 commit eda06c8

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Library
107107
at the end if the FileInput was opened with binary mode.
108108
Patch by Ryosuke Ito.
109109

110+
- Issue #25569: Fix memory leak in SSLSocket.getpeercert().
111+
110112
- Issue #21827: Fixed textwrap.dedent() for the case when largest common
111113
whitespace is a substring of smallest leading whitespace.
112114
Based on patch by Robert Li.

Modules/_ssl.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,25 +1027,23 @@ _get_aia_uri(X509 *certificate, int nid) {
10271027
static PyObject *
10281028
_get_crl_dp(X509 *certificate) {
10291029
STACK_OF(DIST_POINT) *dps;
1030-
int i, j, result;
1031-
PyObject *lst;
1030+
int i, j;
1031+
PyObject *lst, *res = NULL;
10321032

10331033
#if OPENSSL_VERSION_NUMBER < 0x10001000L
1034-
dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points,
1035-
NULL, NULL);
1034+
dps = X509_get_ext_d2i(certificate, NID_crl_distribution_points, NULL, NULL);
10361035
#else
10371036
/* Calls x509v3_cache_extensions and sets up crldp */
10381037
X509_check_ca(certificate);
10391038
dps = certificate->crldp;
10401039
#endif
10411040

1042-
if (dps == NULL) {
1041+
if (dps == NULL)
10431042
return Py_None;
1044-
}
10451043

1046-
if ((lst = PyList_New(0)) == NULL) {
1047-
return NULL;
1048-
}
1044+
lst = PyList_New(0);
1045+
if (lst == NULL)
1046+
goto done;
10491047

10501048
for (i=0; i < sk_DIST_POINT_num(dps); i++) {
10511049
DIST_POINT *dp;
@@ -1058,6 +1056,7 @@ _get_crl_dp(X509 *certificate) {
10581056
GENERAL_NAME *gn;
10591057
ASN1_IA5STRING *uri;
10601058
PyObject *ouri;
1059+
int err;
10611060

10621061
gn = sk_GENERAL_NAME_value(gns, j);
10631062
if (gn->type != GEN_URI) {
@@ -1066,28 +1065,25 @@ _get_crl_dp(X509 *certificate) {
10661065
uri = gn->d.uniformResourceIdentifier;
10671066
ouri = PyUnicode_FromStringAndSize((char *)uri->data,
10681067
uri->length);
1069-
if (ouri == NULL) {
1070-
Py_DECREF(lst);
1071-
return NULL;
1072-
}
1073-
result = PyList_Append(lst, ouri);
1068+
if (ouri == NULL)
1069+
goto done;
1070+
1071+
err = PyList_Append(lst, ouri);
10741072
Py_DECREF(ouri);
1075-
if (result < 0) {
1076-
Py_DECREF(lst);
1077-
return NULL;
1078-
}
1073+
if (err < 0)
1074+
goto done;
10791075
}
10801076
}
1081-
/* convert to tuple or None */
1082-
if (PyList_Size(lst) == 0) {
1083-
Py_DECREF(lst);
1084-
return Py_None;
1085-
} else {
1086-
PyObject *tup;
1087-
tup = PyList_AsTuple(lst);
1088-
Py_DECREF(lst);
1089-
return tup;
1090-
}
1077+
1078+
/* Convert to tuple. */
1079+
res = (PyList_GET_SIZE(lst) > 0) ? PyList_AsTuple(lst) : Py_None;
1080+
1081+
done:
1082+
Py_XDECREF(lst);
1083+
#if OPENSSL_VERSION_NUMBER < 0x10001000L
1084+
sk_DIST_POINT_free(dsp);
1085+
#endif
1086+
return res;
10911087
}
10921088

10931089
static PyObject *

0 commit comments

Comments
 (0)