Skip to content

BUG: avoid uninitialized memory access / NULL-pointer deref in scalar byteswap (#32254) - #32290

Merged
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-32254
Aug 14, 2026
Merged

BUG: avoid uninitialized memory access / NULL-pointer deref in scalar byteswap (#32254)#32290
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-32254

Conversation

@charris

@charris charris commented Aug 14, 2026

Copy link
Copy Markdown
Member

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:

>>> np.bytes_(b"1234").byteswap()
np.bytes_(b'\xb0\xf5D\x01')
>>> np.array([b"1234"]).byteswap()
array([b'1234'], dtype='|S4')

The first result is uninitialized heap garbage. With a debug build:

>>> import numpy as np
>>> np.bytes_(b"1234").byteswap()
Assertion failed: (arr != NULL), function STRING_copyswap, file arraytypes.c.src, line 2507.
[1]    84175 abort      spin python

If there are object dtypes involved, this bug can also lead to a segfault:

>>> import numpy as np
... o = object()
... s = np.array([(o,)], dtype=[("x", "O")])[0]
... print(s.byteswap()["x"])
...
[1]    84728 segmentation fault  python

(this is also an assert in VOID_copyswap in 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.

@charris charris added this to the 2.5.3 release milestone Aug 14, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Aug 14, 2026
@charris
charris merged commit ff7c839 into numpy:maintenance/2.5.x Aug 14, 2026
88 of 90 checks passed
@charris
charris deleted the backport-32254 branch August 14, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants