Skip to content

Commit ba1bf20

Browse files
committed
fix various issues
1 parent d8de992 commit ba1bf20

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Modules/_ssl.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ static PyType_Spec sslerror_type_spec = {
472472
* This stores NULL or new references to Unicode objects in 'lib' and 'reason',
473473
* and thus the caller is responsible for calling Py_XDECREF() on them.
474474
*
475-
* This returns 0 if both 'lib' and 'reason' were successfully set,
476-
* and -1 otherwise.
475+
* This returns 0 if both 'lib' and 'reason' were successfully set (possibly
476+
* to NULL), and -1 otherwise.
477477
*/
478478
static int
479479
ssl_error_fetch_lib_and_reason(_sslmodulestate *state, py_ssl_errcode errcode,
@@ -487,7 +487,6 @@ ssl_error_fetch_lib_and_reason(_sslmodulestate *state, py_ssl_errcode errcode,
487487
if (key == NULL) {
488488
return -1;
489489
}
490-
491490
rc = PyDict_GetItemRef(state->lib_codes_to_names, key, lib);
492491
Py_DECREF(key);
493492
if (rc < 0) {
@@ -503,7 +502,6 @@ ssl_error_fetch_lib_and_reason(_sslmodulestate *state, py_ssl_errcode errcode,
503502
if (rc < 0) {
504503
return -1;
505504
}
506-
507505
return 0;
508506
}
509507

@@ -527,7 +525,7 @@ format_ssl_error_message(PyObject *lib, PyObject *reason, PyObject *verify,
527525
CHECK_OBJECT(reason);
528526
CHECK_OBJECT(verify);
529527
#undef CHECK_OBJECT
530-
#define OPTIONAL_UTF8(x) ((x) == NULL ? PyUnicode_AsUTF8((x)) : NULL)
528+
#define OPTIONAL_UTF8(x) ((x) == NULL ? NULL : PyUnicode_AsUTF8((x)))
531529
const char *libstr = OPTIONAL_UTF8(lib);
532530
const char *reastr = OPTIONAL_UTF8(reason);
533531
const char *verstr = OPTIONAL_UTF8(verify);
@@ -595,18 +593,20 @@ build_ssl_simple_error(_sslmodulestate *state, PyObject *exc_type, int ssl_errno
595593
if (lib == NULL && reason == NULL) {
596594
return exc;
597595
}
598-
PyObject *exc_dict = PyObject_GenericGetDict(exc, NULL); // borrowed
596+
PyObject *exc_dict = PyObject_GenericGetDict(exc, NULL);
599597
if (exc_dict == NULL) {
600598
goto fail;
601599
}
602600
if (lib && PyDict_SetItem(exc_dict, state->str_library, lib) < 0) {
603601
goto fail;
604602
}
605-
if (reason && PyDict_SetItem(exc_dict, state->str_library, reason) < 0) {
603+
if (reason && PyDict_SetItem(exc_dict, state->str_reason, reason) < 0) {
606604
goto fail;
607605
}
606+
Py_DECREF(exc_dict);
608607
return exc;
609608
fail:
609+
Py_XDECREF(exc_dict);
610610
Py_XDECREF(exc);
611611
return NULL;
612612
}
@@ -620,7 +620,7 @@ build_ssl_verify_error(_sslmodulestate *state, PySSLSocket *sslsock, int ssl_err
620620
const char *filename, int lineno)
621621
{
622622
assert(sslsock != NULL);
623-
PyObject *exc = NULL, *verify = NULL, *verify_code = NULL;
623+
PyObject *exc = NULL, *exc_dict = NULL, *verify = NULL, *verify_code = NULL;
624624
/* verify code for cert validation error */
625625
long verify_code_value = SSL_get_verify_result(sslsock->ssl);
626626

@@ -644,7 +644,7 @@ build_ssl_verify_error(_sslmodulestate *state, PySSLSocket *sslsock, int ssl_err
644644
what, hostname
645645
);
646646
}
647-
if (verify == NULL || verify_code == NULL) {
647+
if (verify == NULL) {
648648
goto fail;
649649
}
650650
verify_code = PyLong_FromLong(verify_code_value);
@@ -669,12 +669,11 @@ build_ssl_verify_error(_sslmodulestate *state, PySSLSocket *sslsock, int ssl_err
669669
goto fail;
670670
}
671671
/* build attributes */
672-
PyObject *exc_dict = PyObject_GenericGetDict(exc, NULL); // borrowed
673-
assert(exc_dict != NULL);
672+
exc_dict = PyObject_GenericGetDict(exc, NULL);
674673
if (exc_dict == NULL) {
675674
goto fail;
676675
}
677-
#define SET_ATTR(a, x) \
676+
#define SET_ATTR(a, x) \
678677
do { \
679678
if ((x) && PyDict_SetItem(exc_dict, (a), (x)) < 0) { \
680679
goto fail; \
@@ -684,11 +683,13 @@ build_ssl_verify_error(_sslmodulestate *state, PySSLSocket *sslsock, int ssl_err
684683
SET_ATTR(state->str_reason, reason);
685684
SET_ATTR(state->str_verify_message, verify);
686685
SET_ATTR(state->str_verify_code, verify_code);
687-
#undef SET_ATTR
686+
#undef SET_ATTR
687+
Py_DECREF(exc_dict);
688688
Py_DECREF(verify_code);
689689
Py_DECREF(verify);
690690
return exc;
691691
fail:
692+
Py_XDECREF(exc_dict);
692693
Py_XDECREF(verify_code);
693694
Py_XDECREF(verify);
694695
Py_XDECREF(exc);
@@ -729,9 +730,8 @@ fill_and_set_sslerror(_sslmodulestate *state,
729730
}
730731
Py_XDECREF(reason);
731732
Py_XDECREF(lib);
732-
if (exc != NULL) {
733-
PyErr_SetObject(exc_type, exc);
734-
}
733+
PyErr_SetObject(exc_type, exc);
734+
Py_XDECREF(exc);
735735
}
736736

737737
static int

0 commit comments

Comments
 (0)