gh-146287: Fix signed/unsigned mismatch in _hashlib_hmac_digest_size#148407
Merged
gpshead merged 2 commits intopython:mainfrom Apr 12, 2026
Merged
gh-146287: Fix signed/unsigned mismatch in _hashlib_hmac_digest_size#148407gpshead merged 2 commits intopython:mainfrom
gpshead merged 2 commits intopython:mainfrom
Conversation
…gned wrapping Change _hashlib_hmac_digest_size() return type from unsigned int to int so that a hypothetical negative return from EVP_MD_size() is not silently wrapped to a large positive value. Add an explicit check for negative digest_size in the legacy OpenSSL path, and use SystemError (not ValueError) since these conditions indicate internal invariant violations. Also add debug-build asserts to EVP_get_block_size and EVP_get_digest_size documenting that the hash context is always initialized.
Member
Author
|
there's nothing observable to users in this change, so skip news added. |
picnixz
reviewed
Apr 11, 2026
picnixz
reviewed
Apr 11, 2026
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_hashlib_hmac_digest_size()returnsunsigned int, but on the legacy OpenSSL 1.1.1 path it gets its value fromEVP_MD_size()which returnsint. A negative error return (-1) would silently wrap to UINT_MAX, bypassing the== 0error check and propagating a bogus size to callers.This can't be triggered through the Python API today (the
mdpointer is always validated before use), but the type mismatch means the safety net has a hole._hashlib_hmac_digest_size()return type toint< 0guard in the legacy OpenSSL pathSystemErrorinstead ofValueErrorfor these checks, sincethey represent internal invariant violations
assert()toEVP_get_block_sizeandEVP_get_digest_sizeto document that the hash context is always initialized
intinstead ofunsigned int_hashopenssl.c: Missing negative return check inEVP_get_block_sizeandEVP_get_digest_size#146287fixes #146287