BUG: avoid uninitialized memory access / NULL-pointer deref in scalar byteswap - #32254
Merged
Conversation
ngoldbaum
force-pushed
the
fix-scalar-byteswap
branch
from
August 11, 2026 19:42
cfeb71f to
7515728
Compare
ngoldbaum
marked this pull request as ready for review
August 11, 2026 19:43
mhvk
approved these changes
Aug 11, 2026
mhvk
left a comment
Contributor
There was a problem hiding this comment.
This looks nice, and I would not shorten it. In a way, it is good to see the different paths.
ikrommyd
approved these changes
Aug 12, 2026
ikrommyd
left a comment
Member
There was a problem hiding this comment.
Looks good, left only one comment if you wanna avoid heap garbage in an edge case.
| } | ||
| else { | ||
| copyswap(newmem, data, 1, NULL); | ||
| char *newmem = PyMem_Malloc(descr->elsize); |
Member
There was a problem hiding this comment.
Suggested change
| char *newmem = PyMem_Malloc(descr->elsize); | |
| char *newmem = PyMem_Calloc(1, descr->elsize); |
just to avoid heap garbage in something like this
In [2]: dt = np.dtype([('a', 'i1'), ('b', 'f8')], align=True)
...: np.zeros(2, dt)[0].byteswap().tobytes()
Out[2]: b'\x00\x8e\x7f\x0c\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'vs with calloc
In [1]: dt = np.dtype([('a', 'i1'), ('b', 'f8')], align=True)
...: np.zeros(2, dt)[0].byteswap().tobytes()
Out[1]: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
ngoldbaum
force-pushed
the
fix-scalar-byteswap
branch
from
August 12, 2026 15:58
a8ff3eb to
e14aeca
Compare
Member
Author
|
Thanks for the quick review all! |
charris
pushed a commit
that referenced
this pull request
Aug 14, 2026
charris
added a commit
that referenced
this pull request
Aug 14, 2026
BUG: avoid uninitialized memory access / NULL-pointer deref in scalar byteswap (#32254)
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.