BUG: avoid uninitialized memory access / NULL-pointer deref in scalar byteswap (#32254) - #32290
Merged
Merged
Conversation
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.
PR summary
Fixes #24694
The current scalar byteswap implementation assumes copyswap doesn't need the last argument for the copyswap function, which is an array object. That is incorrect for flexible descriptors (string, void) and for descriptors that request array initialization like object dtype or void dtypes with embedded object dtypes.
For flexible descriptors this allows access to unitialized heap memory on release builds of NumPy or an assert on a debug build:
The first result is uninitialized heap garbage. With a debug build:
If there are object dtypes involved, this bug can also lead to a segfault:
(this is also an assert in
VOID_copyswapin debug builds).I could make this less branchy by forcing all dtypes to go through the object dtype path or making e.g. ints, floats, and other simple data dtypes use the stack-allocated array path. I did find leaving the fast-path, even with all the branches, was still worth it. Scalar byteswaps probably aren't performance critical though so I'm open to simplifying this if anyone wants that.
This was originally noticed by @ikrommyd during review of #32150: #32150 (comment)
AI Disclosure
I used an AI for review and to help identify corner cases.