Skip to content

Improve _hashlib exception reporting when an OpenSSL error occurred #135234

Closed
@picnixz

Description

@picnixz

OpenSSL memory allocation failures

When an OpenSSL error occurs, we usually raise a ValueError. However, failures may be related to malloc(), in which case we should raise a MemoryError as we usually do. I have a PR ready for this.

Incorrect usage of get_openssl_evp_md_by_utf8name()

In get_openssl_evp_md_by_utf8name, when we pass an incorrect Py_hash_type or a digest that we cannot find, we raise:

    raise_ssl_error(state->unsupported_digestmod_error,
                    "unsupported hash type %s", name);

The "unsupported hash type %s" message only happens if no SSL error occurred during the execution, and this only happens if we pass an incorrect Py_hash_type, which also only happens "internally". So we should correctly report that the issue is with the Py_hash_type argument, not with the named argument.

Note: The raise_ssl_error function is a function that raises an automatically formatted message if there is an OpenSSL error or raises the "alternative" error message. But in this case, we should probably separate the exception and raise an SystemError / Py_UNREACHABLE() if we pass a wrong Py_hash_type (strictly speaking, it shouldn't be possible because the compiler would have complained if we pass an arbitrary integer as Py_hash_type is an enum).

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

This is related to the work I did in #134531.

Linked PRs

Metadata

Metadata

Assignees

Labels

extension-modulesC modules in the Modules dirtype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions