Skip to content

Commit 4a2ed1d

Browse files
committed
post-merge
1 parent 7772113 commit 4a2ed1d

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

Modules/_ssl.c

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -466,40 +466,47 @@ static PyType_Spec sslerror_type_spec = {
466466
.slots = sslerror_type_slots
467467
};
468468

469+
/*
470+
* Convert an error code in openssl/err.h to a hash table key.
471+
*
472+
* The 'code' may be a single field component (library, function
473+
* or reason) given as an `int` or a packed error code given as
474+
* an `unsigned long`.
475+
*
476+
* We always assume that 'code' is non-negative.
477+
*/
478+
static inline const void *
479+
ssl_errcode_to_ht_key(ssize_t code)
480+
{
481+
assert(code >= 0); /* individual codes are int but always >= 0*/
482+
return ((const void *)((uintptr_t)(code)));
483+
}
484+
469485
/*
470486
* Get the library and reason strings from a packed error code.
471487
*
472488
* This stores NULL or new references to Unicode objects in 'lib' and 'reason',
473489
* and thus the caller is responsible for calling Py_XDECREF() on them.
474490
*
475-
* This returns 0 if both 'lib' and 'reason' were successfully set (possibly
476-
* to NULL), and -1 otherwise.
491+
* This function always succeeds.
477492
*/
478-
static int
493+
static void
479494
ssl_error_fetch_lib_and_reason(_sslmodulestate *state, py_ssl_errcode errcode,
480495
PyObject **lib, PyObject **reason)
481496
{
482-
int errlib = ERR_GET_LIB(errcode);
483-
int errrea = ERR_GET_REASON(errcode);
497+
const void *key, *val;
498+
int libcode = ERR_GET_LIB(errcode);
499+
int reacode = ERR_GET_REASON(errcode);
484500

485-
const void *key1 = (const void *)((uintptr_t)errlib);
486-
const void *val1 = _Py_hashtable_get(state->lib_codes_to_names, key1);
487-
if (val1 == NULL) {
488-
*lib = NULL;
489-
return -1;
490-
}
491-
assert(PyUnicode_CheckExact(val1));
492-
*lib = Py_NewRef((PyObject *)val1);
501+
key = ssl_errcode_to_ht_key(libcode);
502+
val = (PyObject *)_Py_hashtable_get(state->lib_codes_to_names, key);
503+
assert(val == NULL || PyUnicode_CheckExact(val));
504+
*lib = Py_XNewRef(val);
493505

494-
const void *key2 = (const void *)((uintptr_t)ERR_PACK(errlib, 0, errrea));
495-
const void *val2 = _Py_hashtable_get(state->err_codes_to_names, key2);
496-
if (val2 == NULL) {
497-
*reason = NULL;
498-
return -1;
499-
}
500-
assert(PyUnicode_CheckExact(val2));
501-
*reason = Py_NewRef((PyObject *)val2);
502-
return 0;
506+
key = ssl_errcode_to_ht_key(ERR_PACK(libcode, 0UL, reacode));
507+
val = (PyObject *)_Py_hashtable_get(state->err_codes_to_names, key);
508+
assert(val == NULL || PyUnicode_CheckExact(val));
509+
*reason = Py_XNewRef(val);
503510
}
504511

505512
/*
@@ -749,11 +756,7 @@ fill_and_set_sslerror(_sslmodulestate *state,
749756
{
750757
PyObject *lib = NULL, *reason = NULL;
751758
if (errcode) {
752-
if (ssl_error_fetch_lib_and_reason(state, errcode, &lib, &reason) < 0) {
753-
Py_XDECREF(reason);
754-
Py_XDECREF(lib);
755-
return;
756-
}
759+
ssl_error_fetch_lib_and_reason(state, errcode, &lib, &reason);
757760
}
758761
if (errstr == NULL) {
759762
errstr = errcode
@@ -6855,7 +6858,7 @@ py_ht_errcode_to_name_create(void) {
68556858

68566859
for (const py_ssl_error_code *p = error_codes; p->mnemonic != NULL; p++) {
68576860
py_ssl_errcode code = ERR_PACK(p->library, 0, p->reason);
6858-
const void *key = (const void *)((uintptr_t)code);
6861+
const void *key = ssl_errcode_to_ht_key(code);
68596862
PyObject *value = PyUnicode_FromString(p->mnemonic);
68606863
if (value == NULL) {
68616864
goto error;
@@ -6903,7 +6906,7 @@ py_ht_libcode_to_name_create(void) {
69036906
}
69046907

69056908
for (const py_ssl_library_code *p = library_codes; p->library != NULL; p++) {
6906-
const void *key = (const void *)((uintptr_t)p->code);
6909+
const void *key = ssl_errcode_to_ht_key(p->code);
69076910
PyObject *value = PyUnicode_FromString(p->library);
69086911
if (value == NULL) {
69096912
goto error;

0 commit comments

Comments
 (0)