From 23eb1fe74c40952c0b528afdb786549599c28ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:44:07 +0100 Subject: [PATCH 01/15] fix error-branches in hashlib --- Modules/_hashopenssl.c | 230 ++++++++++++++++++++++++++--------------- 1 file changed, 144 insertions(+), 86 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 2c9a9feecc79f0..57cab377d6da25 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -300,28 +300,13 @@ class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module)) /* LCOV_EXCL_START */ static PyObject * -_setException(PyObject *exc, const char* altmsg, ...) +format_openssl_error_code(PyObject *exc, unsigned long errcode) { - unsigned long errcode = ERR_peek_last_error(); - const char *lib, *func, *reason; - va_list vargs; - - va_start(vargs, altmsg); - if (!errcode) { - if (altmsg == NULL) { - PyErr_SetString(exc, "no reason supplied"); - } else { - PyErr_FormatV(exc, altmsg, vargs); - } - va_end(vargs); - return NULL; - } - va_end(vargs); - ERR_clear_error(); + assert(errcode != 0); - lib = ERR_lib_error_string(errcode); - func = ERR_func_error_string(errcode); - reason = ERR_reason_error_string(errcode); + const char *lib = ERR_lib_error_string(errcode); + const char *func = ERR_func_error_string(errcode); + const char *reason = ERR_reason_error_string(errcode); if (lib && func) { PyErr_Format(exc, "[%s: %s] %s", lib, func, reason); @@ -334,6 +319,40 @@ _setException(PyObject *exc, const char* altmsg, ...) } return NULL; } + +static PyObject * +format_openssl_exception(PyObject *exc, const char *alt_format, ...) +{ + assert(alt_format != NULL); + unsigned long errcode = ERR_peek_last_error(); + if (!errcode) { + va_list vargs; + va_start(vargs, alt_format); + PyErr_FormatV(exc, alt_format, vargs); + va_end(vargs); + return NULL; + } + ERR_clear_error(); + return format_openssl_error_code(exc, errcode); +} + +static void +set_openssl_exception(PyObject *exc, const char *alt_message) +{ + unsigned long errcode = ERR_peek_last_error(); + if (!errcode) { + PyErr_SetString(exc, alt_message); + return; + } + ERR_clear_error(); + (void)format_openssl_error_code(exc, errcode); +} + +static inline void +set_simple_openssl_exception(void) +{ + set_openssl_exception(PyExc_ValueError, "no reason supplied"); +} /* LCOV_EXCL_STOP */ static PyObject* @@ -407,7 +426,8 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht) } } if (digest == NULL) { - _setException(state->unsupported_digestmod_error, "unsupported hash type %s", name); + format_openssl_exception(state->unsupported_digestmod_error, + "unsupported hash type %s", name); return NULL; } return digest; @@ -448,12 +468,7 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type return NULL; } - evp = py_digest_by_name(module, name, py_ht); - if (evp == NULL) { - return NULL; - } - - return evp; + return py_digest_by_name(module, name, py_ht); } static EVPobject * @@ -486,7 +501,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) else process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return -1; } len -= process; @@ -535,7 +550,8 @@ EVP_copy_impl(EVPobject *self) if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) { Py_DECREF(newobj); - return _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); + return NULL; } return (PyObject *)newobj; } @@ -562,17 +578,21 @@ EVP_digest_impl(EVPobject *self) } if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) { - return _setException(PyExc_ValueError, NULL); + goto error; } digest_size = EVP_MD_CTX_size(temp_ctx); if (!EVP_DigestFinal(temp_ctx, digest, NULL)) { - _setException(PyExc_ValueError, NULL); - return NULL; + goto error; } retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); EVP_MD_CTX_free(temp_ctx); return retval; + +error: + EVP_MD_CTX_free(temp_ctx); + set_simple_openssl_exception(); + return NULL; } /*[clinic input] @@ -597,17 +617,21 @@ EVP_hexdigest_impl(EVPobject *self) /* Get the raw (binary) digest value */ if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) { - return _setException(PyExc_ValueError, NULL); + goto error; } digest_size = EVP_MD_CTX_size(temp_ctx); if (!EVP_DigestFinal(temp_ctx, digest, NULL)) { - _setException(PyExc_ValueError, NULL); - return NULL; + goto error; } EVP_MD_CTX_free(temp_ctx); return _Py_strhex((const char *)digest, (Py_ssize_t)digest_size); + +error: + EVP_MD_CTX_free(temp_ctx); + set_simple_openssl_exception(); + return NULL; } /*[clinic input] @@ -675,7 +699,8 @@ EVP_get_digest_size(EVPobject *self, void *closure) static PyObject * EVP_get_name(EVPobject *self, void *closure) { - return py_digest_name(EVP_MD_CTX_md(self->ctx)); + const EVP_MD *md = EVP_MD_CTX_md(self->ctx); + return md == NULL ? set_simple_openssl_exception() : py_digest_name(md); } static PyGetSetDef EVP_getseters[] = { @@ -699,7 +724,7 @@ static PyObject * EVP_repr(EVPobject *self) { PyObject *name_obj, *repr; - name_obj = py_digest_name(EVP_MD_CTX_md(self->ctx)); + name_obj = EVP_get_name(self, NULL); if (!name_obj) { return NULL; } @@ -773,21 +798,22 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length) } if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) { - Py_DECREF(retval); - EVP_MD_CTX_free(temp_ctx); - return _setException(PyExc_ValueError, NULL); + goto error; } if (!EVP_DigestFinalXOF(temp_ctx, (unsigned char*)PyBytes_AS_STRING(retval), length)) { - Py_DECREF(retval); - EVP_MD_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + goto error; } EVP_MD_CTX_free(temp_ctx); return retval; + +error: + Py_DECREF(retval); + EVP_MD_CTX_free(temp_ctx); + set_simple_openssl_exception(); + return NULL; } /*[clinic input] @@ -821,15 +847,10 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length) /* Get the raw (binary) digest value */ if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) { - PyMem_Free(digest); - EVP_MD_CTX_free(temp_ctx); - return _setException(PyExc_ValueError, NULL); + goto error; } if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) { - PyMem_Free(digest); - EVP_MD_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + goto error; } EVP_MD_CTX_free(temp_ctx); @@ -837,6 +858,12 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length) retval = _Py_strhex((const char *)digest, length); PyMem_Free(digest); return retval; + +error: + PyMem_Free(digest); + EVP_MD_CTX_free(temp_ctx); + set_simple_openssl_exception(); + return NULL; } static PyMethodDef EVPXOF_methods[] = { @@ -913,7 +940,10 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, module, digestname, usedforsecurity ? Py_ht_evp : Py_ht_evp_nosecurity ); if (digest == NULL) { - goto exit; + if (data_obj != NULL) { + PyBuffer_Release(&view); + } + return NULL; } if ((EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF) { @@ -937,9 +967,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, int result = EVP_DigestInit_ex(self->ctx, digest, NULL); if (!result) { - _setException(PyExc_ValueError, NULL); - Py_CLEAR(self); - goto exit; + goto error; } if (view.buf && view.len) { @@ -953,20 +981,22 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, result = EVP_hash(self, view.buf, view.len); } if (result == -1) { - Py_CLEAR(self); - goto exit; + goto error; } } - exit: +exit: if (data_obj != NULL) { PyBuffer_Release(&view); } - if (digest != NULL) { - PY_EVP_MD_free(digest); - } - + assert(digest != NULL); + PY_EVP_MD_free(digest); return (PyObject *)self; + +error: + Py_CLEAR(self); + set_simple_openssl_exception(); + goto exit; } @@ -1327,7 +1357,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name, if (!retval) { Py_CLEAR(key_obj); - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); goto end; } @@ -1433,7 +1463,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, /* let OpenSSL validate the rest */ retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0); if (!retval) { - _setException(PyExc_ValueError, "Invalid parameter combination for n, r, p, maxmem."); + set_openssl_exception(PyExc_ValueError, + "Invalid parameter combination for n, r, p, maxmem."); return NULL; } @@ -1454,7 +1485,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, if (!retval) { Py_CLEAR(key_obj); - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return NULL; } return key_obj; @@ -1511,7 +1542,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key, PY_EVP_MD_free(evp); if (result == NULL) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return NULL; } return PyBytes_FromStringAndSize((const char*)md, md_len); @@ -1537,7 +1568,6 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, PyObject *digestmod) /*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/ { - PyTypeObject *type = get_hashlib_state(module)->HMACtype; PY_EVP_MD *digest; HMAC_CTX *ctx = NULL; HMACobject *self = NULL; @@ -1562,8 +1592,9 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, ctx = HMAC_CTX_new(); if (ctx == NULL) { - _setException(PyExc_ValueError, NULL); - goto error; + PY_EVP_MD_free(digest); + set_simple_openssl_exception(); + return NULL; } r = HMAC_Init_ex( @@ -1574,11 +1605,12 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, NULL /*impl*/); PY_EVP_MD_free(digest); if (r == 0) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); goto error; } - self = (HMACobject *)PyObject_New(HMACobject, type); + PyTypeObject *type = get_hashlib_state(module)->HMACtype; + self = PyObject_New(HMACobject, type); if (self == NULL) { goto error; } @@ -1594,8 +1626,9 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, return (PyObject*)self; error: - if (ctx) HMAC_CTX_free(ctx); - if (self) PyObject_Free(self); + assert(ctx != NULL); + HMAC_CTX_free(ctx); + PyObject_Free(self); return NULL; } @@ -1610,11 +1643,20 @@ locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self) return result; } +/* returning 0 means that an error occurred and an OpenSSL exception is set */ static unsigned int _hmac_digest_size(HMACobject *self) { - unsigned int digest_size = EVP_MD_size(HMAC_CTX_get_md(self->ctx)); + const EVP_MD *md = HMAC_CTX_get_md(self->ctx); + if (md == NULL) { + set_simple_openssl_exception(); + return 0; + } + unsigned int digest_size = EVP_MD_size(md); assert(digest_size <= EVP_MAX_MD_SIZE); + if (digest_size == 0) { + set_simple_openssl_exception(); + } return digest_size; } @@ -1642,7 +1684,7 @@ _hmac_update(HMACobject *self, PyObject *obj) PyBuffer_Release(&view); if (r == 0) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return 0; } return 1; @@ -1662,11 +1704,13 @@ _hashlib_HMAC_copy_impl(HMACobject *self) HMAC_CTX *ctx = HMAC_CTX_new(); if (ctx == NULL) { - return _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); + return NULL; } if (!locked_HMAC_CTX_copy(ctx, self)) { HMAC_CTX_free(ctx); - return _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); + return NULL; } retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self)); @@ -1692,7 +1736,12 @@ _hmac_dealloc(HMACobject *self) static PyObject * _hmac_repr(HMACobject *self) { - PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx)); + const EVP_MD *md = HMAC_CTX_get_md(self->ctx); + if (md == NULL) { + set_simple_openssl_exception(); + return NULL; + } + PyObject *digest_name = py_digest_name(md); if (digest_name == NULL) { return NULL; } @@ -1729,13 +1778,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len) return 0; } if (!locked_HMAC_CTX_copy(temp_ctx, self)) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return 0; } int r = HMAC_Final(temp_ctx, buf, &len); HMAC_CTX_free(temp_ctx); if (r == 0) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return 0; } return 1; @@ -1753,7 +1802,8 @@ _hashlib_HMAC_digest_impl(HMACobject *self) unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int digest_size = _hmac_digest_size(self); if (digest_size == 0) { - return _setException(PyExc_ValueError, NULL); + assert(PyErr_Occurred()); + return NULL; } int r = _hmac_digest(self, digest, digest_size); if (r == 0) { @@ -1778,7 +1828,8 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self) unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int digest_size = _hmac_digest_size(self); if (digest_size == 0) { - return _setException(PyExc_ValueError, NULL); + assert(PyErr_Occurred()); + return NULL; } int r = _hmac_digest(self, digest, digest_size); if (r == 0) { @@ -1792,7 +1843,8 @@ _hashlib_hmac_get_digest_size(HMACobject *self, void *closure) { unsigned int digest_size = _hmac_digest_size(self); if (digest_size == 0) { - return _setException(PyExc_ValueError, NULL); + assert(PyErr_Occurred()); + return NULL; } return PyLong_FromLong(digest_size); } @@ -1802,7 +1854,8 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - return _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); + return NULL; } return PyLong_FromLong(EVP_MD_block_size(md)); } @@ -1810,7 +1863,12 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure) static PyObject * _hashlib_hmac_get_name(HMACobject *self, void *closure) { - PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx)); + const EVP_MD *md = HMAC_CTX_get_md(self->ctx); + if (md == NULL) { + set_simple_openssl_exception(); + return NULL; + } + PyObject *digest_name = py_digest_name(md); if (digest_name == NULL) { return NULL; } @@ -1961,7 +2019,7 @@ _hashlib_get_fips_mode_impl(PyObject *module) // But 0 is also a valid result value. unsigned long errcode = ERR_peek_last_error(); if (errcode) { - _setException(PyExc_ValueError, NULL); + set_simple_openssl_exception(); return -1; } } From 49e6c96f4b187d518836fe29a6c8604800c6a785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:45:57 +0100 Subject: [PATCH 02/15] small cosmetic changes in error messages --- Modules/_hashopenssl.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 57cab377d6da25..46bee1a535f785 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1433,14 +1433,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, r = PyLong_AsUnsignedLong(r_obj); if (r == (unsigned long) -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, - "r is required and must be an unsigned int"); + "r is required and must be an unsigned int"); return NULL; } p = PyLong_AsUnsignedLong(p_obj); if (p == (unsigned long) -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, - "p is required and must be an unsigned int"); + "p is required and must be an unsigned int"); return NULL; } @@ -1449,14 +1449,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, future. The maxmem constant is private to OpenSSL. */ PyErr_Format(PyExc_ValueError, "maxmem must be positive and smaller than %d", - INT_MAX); + INT_MAX); return NULL; } if (dklen < 1 || dklen > INT_MAX) { PyErr_Format(PyExc_ValueError, - "dklen must be greater than 0 and smaller than %d", - INT_MAX); + "dklen must be greater than 0 and smaller than %d", + INT_MAX); return NULL; } From 87a1e98ca41850761fab823818f212159fc49b12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:31:03 +0100 Subject: [PATCH 03/15] fix compilation --- Modules/_hashopenssl.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 46bee1a535f785..8c0a0d8df72202 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -700,7 +700,11 @@ static PyObject * EVP_get_name(EVPobject *self, void *closure) { const EVP_MD *md = EVP_MD_CTX_md(self->ctx); - return md == NULL ? set_simple_openssl_exception() : py_digest_name(md); + if (md == NULL) { + set_simple_openssl_exception(); + return NULL; + } + return py_digest_name(md); } static PyGetSetDef EVP_getseters[] = { From 1b7c775b6889ecf33b4cf8b6666b37033bbc9531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:37:16 +0100 Subject: [PATCH 04/15] remove unused variable --- Modules/_hashopenssl.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 8c0a0d8df72202..6d73e7bc428871 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -442,7 +442,6 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht) */ static PY_EVP_MD* py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type py_ht) { - PY_EVP_MD* evp; PyObject *name_obj = NULL; const char *name; From 9e34c0e2db3afd692b2746df3e1743bfcdfe956a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 5 Jan 2025 09:39:37 +0100 Subject: [PATCH 05/15] only keep error-branches fixes --- Modules/_hashopenssl.c | 123 +++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 72 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 16e4f87e59fa15..92bf969b05320c 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -300,14 +300,29 @@ class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module)) /* LCOV_EXCL_START */ static PyObject * -format_openssl_error_code(PyObject *exc, unsigned long errcode) +_setException(PyObject *exc, const char* altmsg, ...) { - assert(errcode != 0); + unsigned long errcode = ERR_peek_last_error(); + const char *lib, *func, *reason; + va_list vargs; + + va_start(vargs, altmsg); + if (!errcode) { + if (altmsg == NULL) { + PyErr_SetString(exc, "no reason supplied"); + } else { + PyErr_FormatV(exc, altmsg, vargs); + } + va_end(vargs); + return NULL; + } + va_end(vargs); + ERR_clear_error(); /* ERR_ERROR_STRING(3) ensures that the messages below are ASCII */ - const char *lib = ERR_lib_error_string(errcode); - const char *func = ERR_func_error_string(errcode); - const char *reason = ERR_reason_error_string(errcode); + lib = ERR_lib_error_string(errcode); + func = ERR_func_error_string(errcode); + reason = ERR_reason_error_string(errcode); if (lib && func) { PyErr_Format(exc, "[%s: %s] %s", lib, func, reason); @@ -320,40 +335,6 @@ format_openssl_error_code(PyObject *exc, unsigned long errcode) } return NULL; } - -static PyObject * -format_openssl_exception(PyObject *exc, const char *alt_format, ...) -{ - assert(alt_format != NULL); - unsigned long errcode = ERR_peek_last_error(); - if (!errcode) { - va_list vargs; - va_start(vargs, alt_format); - PyErr_FormatV(exc, alt_format, vargs); - va_end(vargs); - return NULL; - } - ERR_clear_error(); - return format_openssl_error_code(exc, errcode); -} - -static void -set_openssl_exception(PyObject *exc, const char *alt_message) -{ - unsigned long errcode = ERR_peek_last_error(); - if (!errcode) { - PyErr_SetString(exc, alt_message); - return; - } - ERR_clear_error(); - (void)format_openssl_error_code(exc, errcode); -} - -static inline void -set_simple_openssl_exception(void) -{ - set_openssl_exception(PyExc_ValueError, "no reason supplied"); -} /* LCOV_EXCL_STOP */ static PyObject* @@ -427,8 +408,7 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht) } } if (digest == NULL) { - format_openssl_exception(state->unsupported_digestmod_error, - "unsupported hash type %s", name); + _setException(state->unsupported_digestmod_error, "unsupported hash type %s", name); return NULL; } return digest; @@ -501,7 +481,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) else process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return -1; } len -= process; @@ -550,7 +530,7 @@ EVP_copy_impl(EVPobject *self) if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) { Py_DECREF(newobj); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } return (PyObject *)newobj; @@ -591,7 +571,7 @@ EVP_digest_impl(EVPobject *self) error: EVP_MD_CTX_free(temp_ctx); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } @@ -630,7 +610,7 @@ EVP_hexdigest_impl(EVPobject *self) error: EVP_MD_CTX_free(temp_ctx); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } @@ -701,7 +681,7 @@ EVP_get_name(EVPobject *self, void *closure) { const EVP_MD *md = EVP_MD_CTX_md(self->ctx); if (md == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } return py_digest_name(md); @@ -816,7 +796,7 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length) error: Py_DECREF(retval); EVP_MD_CTX_free(temp_ctx); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } @@ -866,7 +846,7 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length) error: PyMem_Free(digest); EVP_MD_CTX_free(temp_ctx); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } @@ -999,7 +979,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, error: Py_CLEAR(self); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); goto exit; } @@ -1361,7 +1341,7 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name, if (!retval) { Py_CLEAR(key_obj); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); goto end; } @@ -1437,14 +1417,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, r = PyLong_AsUnsignedLong(r_obj); if (r == (unsigned long) -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, - "r is required and must be an unsigned int"); + "r is required and must be an unsigned int"); return NULL; } p = PyLong_AsUnsignedLong(p_obj); if (p == (unsigned long) -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, - "p is required and must be an unsigned int"); + "p is required and must be an unsigned int"); return NULL; } @@ -1453,22 +1433,21 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, future. The maxmem constant is private to OpenSSL. */ PyErr_Format(PyExc_ValueError, "maxmem must be positive and smaller than %d", - INT_MAX); + INT_MAX); return NULL; } if (dklen < 1 || dklen > INT_MAX) { PyErr_Format(PyExc_ValueError, - "dklen must be greater than 0 and smaller than %d", - INT_MAX); + "dklen must be greater than 0 and smaller than %d", + INT_MAX); return NULL; } /* let OpenSSL validate the rest */ retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0); if (!retval) { - set_openssl_exception(PyExc_ValueError, - "Invalid parameter combination for n, r, p, maxmem."); + _setException(PyExc_ValueError, "Invalid parameter combination for n, r, p, maxmem."); return NULL; } @@ -1489,7 +1468,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, if (!retval) { Py_CLEAR(key_obj); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } return key_obj; @@ -1546,7 +1525,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key, PY_EVP_MD_free(evp); if (result == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } return PyBytes_FromStringAndSize((const char*)md, md_len); @@ -1597,7 +1576,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, ctx = HMAC_CTX_new(); if (ctx == NULL) { PY_EVP_MD_free(digest); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } @@ -1609,7 +1588,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, NULL /*impl*/); PY_EVP_MD_free(digest); if (r == 0) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); goto error; } @@ -1653,13 +1632,13 @@ _hmac_digest_size(HMACobject *self) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return 0; } unsigned int digest_size = EVP_MD_size(md); assert(digest_size <= EVP_MAX_MD_SIZE); if (digest_size == 0) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); } return digest_size; } @@ -1688,7 +1667,7 @@ _hmac_update(HMACobject *self, PyObject *obj) PyBuffer_Release(&view); if (r == 0) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return 0; } return 1; @@ -1708,12 +1687,12 @@ _hashlib_HMAC_copy_impl(HMACobject *self) HMAC_CTX *ctx = HMAC_CTX_new(); if (ctx == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } if (!locked_HMAC_CTX_copy(ctx, self)) { HMAC_CTX_free(ctx); - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } @@ -1742,7 +1721,7 @@ _hmac_repr(HMACobject *self) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } PyObject *digest_name = py_digest_name(md); @@ -1782,13 +1761,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len) return 0; } if (!locked_HMAC_CTX_copy(temp_ctx, self)) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return 0; } int r = HMAC_Final(temp_ctx, buf, &len); HMAC_CTX_free(temp_ctx); if (r == 0) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return 0; } return 1; @@ -1858,7 +1837,7 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } return PyLong_FromLong(EVP_MD_block_size(md)); @@ -1869,7 +1848,7 @@ _hashlib_hmac_get_name(HMACobject *self, void *closure) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return NULL; } PyObject *digest_name = py_digest_name(md); @@ -2023,7 +2002,7 @@ _hashlib_get_fips_mode_impl(PyObject *module) // But 0 is also a valid result value. unsigned long errcode = ERR_peek_last_error(); if (errcode) { - set_simple_openssl_exception(); + _setException(PyExc_ValueError, NULL); return -1; } } From f06a52e8c56d0142809ca3e6401110ddb9230202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 5 Jan 2025 09:46:17 +0100 Subject: [PATCH 06/15] cosmetic alignment updates --- Modules/_hashopenssl.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 92bf969b05320c..1eaefe9d1d4998 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1417,14 +1417,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, r = PyLong_AsUnsignedLong(r_obj); if (r == (unsigned long) -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, - "r is required and must be an unsigned int"); + "r is required and must be an unsigned int"); return NULL; } p = PyLong_AsUnsignedLong(p_obj); if (p == (unsigned long) -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, - "p is required and must be an unsigned int"); + "p is required and must be an unsigned int"); return NULL; } @@ -1433,14 +1433,14 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, future. The maxmem constant is private to OpenSSL. */ PyErr_Format(PyExc_ValueError, "maxmem must be positive and smaller than %d", - INT_MAX); + INT_MAX); return NULL; } if (dklen < 1 || dklen > INT_MAX) { PyErr_Format(PyExc_ValueError, - "dklen must be greater than 0 and smaller than %d", - INT_MAX); + "dklen must be greater than 0 and smaller than %d", + INT_MAX); return NULL; } From 4045980d8893b7798727ba2887090af1c81d9666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:00:23 +0100 Subject: [PATCH 07/15] avoid overwrite exception in `py_evp_fromname` --- Modules/_hashopenssl.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 1eaefe9d1d4998..dd91299ff2f81d 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -951,7 +951,9 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, int result = EVP_DigestInit_ex(self->ctx, digest, NULL); if (!result) { - goto error; + (void)_setException(PyExc_ValueError, NULL); + Py_CLEAR(self); + goto exit; } if (view.buf && view.len) { @@ -965,7 +967,8 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, result = EVP_hash(self, view.buf, view.len); } if (result == -1) { - goto error; + Py_CLEAR(self); + goto exit; } } @@ -976,11 +979,6 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, assert(digest != NULL); PY_EVP_MD_free(digest); return (PyObject *)self; - -error: - Py_CLEAR(self); - _setException(PyExc_ValueError, NULL); - goto exit; } From bf48fc28f68a3c646a791644abef965225cf3961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:01:11 +0100 Subject: [PATCH 08/15] remove un-necessary `PyErr_Occurred()` assertions --- Modules/_hashopenssl.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index dd91299ff2f81d..5432e84dd20fea 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1783,7 +1783,6 @@ _hashlib_HMAC_digest_impl(HMACobject *self) unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int digest_size = _hmac_digest_size(self); if (digest_size == 0) { - assert(PyErr_Occurred()); return NULL; } int r = _hmac_digest(self, digest, digest_size); @@ -1809,7 +1808,6 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self) unsigned char digest[EVP_MAX_MD_SIZE]; unsigned int digest_size = _hmac_digest_size(self); if (digest_size == 0) { - assert(PyErr_Occurred()); return NULL; } int r = _hmac_digest(self, digest, digest_size); @@ -1824,7 +1822,6 @@ _hashlib_hmac_get_digest_size(HMACobject *self, void *closure) { unsigned int digest_size = _hmac_digest_size(self); if (digest_size == 0) { - assert(PyErr_Occurred()); return NULL; } return PyLong_FromLong(digest_size); From 6f80c7c02b5115fb10624fdd18edc587c86b8571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:02:02 +0100 Subject: [PATCH 09/15] suppress unused `_setException()` return value --- Modules/_hashopenssl.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 5432e84dd20fea..ecf58856853a69 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -481,7 +481,7 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) else process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return -1; } len -= process; @@ -1586,7 +1586,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, NULL /*impl*/); PY_EVP_MD_free(digest); if (r == 0) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); goto error; } @@ -1630,13 +1630,13 @@ _hmac_digest_size(HMACobject *self) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return 0; } unsigned int digest_size = EVP_MD_size(md); assert(digest_size <= EVP_MAX_MD_SIZE); if (digest_size == 0) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); } return digest_size; } @@ -1665,7 +1665,7 @@ _hmac_update(HMACobject *self, PyObject *obj) PyBuffer_Release(&view); if (r == 0) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return 0; } return 1; @@ -1759,13 +1759,13 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len) return 0; } if (!locked_HMAC_CTX_copy(temp_ctx, self)) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return 0; } int r = HMAC_Final(temp_ctx, buf, &len); HMAC_CTX_free(temp_ctx); if (r == 0) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return 0; } return 1; @@ -1997,7 +1997,7 @@ _hashlib_get_fips_mode_impl(PyObject *module) // But 0 is also a valid result value. unsigned long errcode = ERR_peek_last_error(); if (errcode) { - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return -1; } } From fc3ebb07ebc94e2d4fc950ed4f2b107d0d625062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:02:16 +0100 Subject: [PATCH 10/15] collapse `_setException() + return NULL` into one statement --- Modules/_hashopenssl.c | 50 +++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index ecf58856853a69..4a6312223e303d 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -408,8 +408,8 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht) } } if (digest == NULL) { - _setException(state->unsupported_digestmod_error, "unsupported hash type %s", name); - return NULL; + return _setException(state->unsupported_digestmod_error, + "unsupported hash type %s", name); } return digest; } @@ -530,8 +530,7 @@ EVP_copy_impl(EVPobject *self) if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) { Py_DECREF(newobj); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } return (PyObject *)newobj; } @@ -571,8 +570,7 @@ EVP_digest_impl(EVPobject *self) error: EVP_MD_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } /*[clinic input] @@ -610,8 +608,7 @@ EVP_hexdigest_impl(EVPobject *self) error: EVP_MD_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } /*[clinic input] @@ -681,8 +678,7 @@ EVP_get_name(EVPobject *self, void *closure) { const EVP_MD *md = EVP_MD_CTX_md(self->ctx); if (md == NULL) { - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } return py_digest_name(md); } @@ -796,8 +792,7 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length) error: Py_DECREF(retval); EVP_MD_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } /*[clinic input] @@ -846,8 +841,7 @@ EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length) error: PyMem_Free(digest); EVP_MD_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } static PyMethodDef EVPXOF_methods[] = { @@ -1445,8 +1439,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, /* let OpenSSL validate the rest */ retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0); if (!retval) { - _setException(PyExc_ValueError, "Invalid parameter combination for n, r, p, maxmem."); - return NULL; + return _setException(PyExc_ValueError, + "Invalid parameter combination for n, r, p, maxmem."); } key_obj = PyBytes_FromStringAndSize(NULL, dklen); @@ -1466,8 +1460,7 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, if (!retval) { Py_CLEAR(key_obj); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } return key_obj; } @@ -1523,8 +1516,7 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key, PY_EVP_MD_free(evp); if (result == NULL) { - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } return PyBytes_FromStringAndSize((const char*)md, md_len); } @@ -1574,8 +1566,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj, ctx = HMAC_CTX_new(); if (ctx == NULL) { PY_EVP_MD_free(digest); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } r = HMAC_Init_ex( @@ -1685,13 +1676,11 @@ _hashlib_HMAC_copy_impl(HMACobject *self) HMAC_CTX *ctx = HMAC_CTX_new(); if (ctx == NULL) { - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } if (!locked_HMAC_CTX_copy(ctx, self)) { HMAC_CTX_free(ctx); - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self)); @@ -1719,8 +1708,7 @@ _hmac_repr(HMACobject *self) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } PyObject *digest_name = py_digest_name(md); if (digest_name == NULL) { @@ -1832,8 +1820,7 @@ _hashlib_hmac_get_block_size(HMACobject *self, void *closure) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } return PyLong_FromLong(EVP_MD_block_size(md)); } @@ -1843,8 +1830,7 @@ _hashlib_hmac_get_name(HMACobject *self, void *closure) { const EVP_MD *md = HMAC_CTX_get_md(self->ctx); if (md == NULL) { - _setException(PyExc_ValueError, NULL); - return NULL; + return _setException(PyExc_ValueError, NULL); } PyObject *digest_name = py_digest_name(md); if (digest_name == NULL) { From 7455d3cb27bc645c6aed61c4502e1406b3385a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:10:30 +0100 Subject: [PATCH 11/15] fix return type --- Modules/_hashopenssl.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 4a6312223e303d..3e7a6575c45ebc 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -408,8 +408,9 @@ py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht) } } if (digest == NULL) { - return _setException(state->unsupported_digestmod_error, - "unsupported hash type %s", name); + (void)_setException(state->unsupported_digestmod_error, + "unsupported hash type %s", name); + return NULL; } return digest; } From add9e43a784d132d515dde38bd7c0f9da661c27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:22:59 +0100 Subject: [PATCH 12/15] [empty commit] trigger RTD build From d72c096d31299cbb0c551c01e6c20a35d531047b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 7 Jan 2025 10:52:37 +0100 Subject: [PATCH 13/15] [empty commit] trigger the CI From bf83b61885c8b468cd80e270b1174001e5bea7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:48:31 +0100 Subject: [PATCH 14/15] post-merge --- Modules/_hashopenssl.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 2d1416219e0d69..3c99de5bfdbf31 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -797,7 +797,8 @@ EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length) } if (!EVP_DigestFinalXOF(temp_ctx, (unsigned char*)PyBytes_AS_STRING(retval), - length)) { + length)) + { goto error; } @@ -930,10 +931,7 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, module, digestname, usedforsecurity ? Py_ht_evp : Py_ht_evp_nosecurity ); if (digest == NULL) { - if (data_obj != NULL) { - PyBuffer_Release(&view); - } - return NULL; + goto exit; } if ((EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF) { @@ -982,8 +980,10 @@ py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj, if (data_obj != NULL) { PyBuffer_Release(&view); } - assert(digest != NULL); - PY_EVP_MD_free(digest); + if (digest != NULL) { + PY_EVP_MD_free(digest); + } + return (PyObject *)self; } @@ -1756,12 +1756,12 @@ _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len) { HMAC_CTX *temp_ctx = HMAC_CTX_new(); if (temp_ctx == NULL) { - PyErr_NoMemory(); + (void)PyErr_NoMemory(); return 0; } if (!locked_HMAC_CTX_copy(temp_ctx, self)) { HMAC_CTX_free(temp_ctx); - _setException(PyExc_ValueError, NULL); + (void)_setException(PyExc_ValueError, NULL); return 0; } int r = HMAC_Final(temp_ctx, buf, &len); From 927ab4c3b47b93c48b05d0b15e06a116137f9b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:50:40 +0100 Subject: [PATCH 15/15] fix comment --- Modules/_hashopenssl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 3c99de5bfdbf31..4d6e4f192f5ad9 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1623,7 +1623,7 @@ locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self) return result; } -/* returning 0 means that an error occurred and an OpenSSL exception is set */ +/* returning 0 means that an error occurred and an exception is set */ static unsigned int _hmac_digest_size(HMACobject *self) {