gh-154566: Fix array.byteswap() corrupting 'Zd' arrays with more than one element#154567
Open
PhysicistJohn wants to merge 1 commit into
Open
gh-154566: Fix array.byteswap() corrupting 'Zd' arrays with more than one element#154567PhysicistJohn wants to merge 1 commit into
PhysicistJohn wants to merge 1 commit into
Conversation
…e than one element The 16-byte-item loop in array_array_byteswap_impl() advanced the buffer pointer by only 8 bytes per iteration, even though each iteration swaps a full 16-byte item (two independent 8-byte halves for the real/imaginary double components). This caused every item after the first to overlap the previous iteration's byte window and come out scrambled. A single byteswap() call gave wrong bytes for the second item onward. Calling byteswap() twice happened to round-trip back to the original value (the corruption is self-canceling under double application), which is why the existing test suite -- which only checked a double-call round-trip -- didn't catch it. Added a regression test that checks the actual byte-level result of a single call.
This comment was marked as resolved.
This comment was marked as resolved.
skirpichev
reviewed
Jul 24, 2026
Comment on lines
+1
to
+6
| Fix :meth:`array.array.byteswap` corrupting data for ``'Zd'`` (complex | ||
| double) arrays with more than one element: the 16-byte item loop advanced | ||
| the buffer pointer by only 8 bytes per iteration, causing items after the | ||
| first to be scrambled. A single :meth:`!byteswap` call produced incorrect | ||
| results while a double call happened to round-trip back to the original | ||
| value, which is why the existing test suite did not catch it. |
Member
There was a problem hiding this comment.
Suggested change
| Fix :meth:`array.array.byteswap` corrupting data for ``'Zd'`` (complex | |
| double) arrays with more than one element: the 16-byte item loop advanced | |
| the buffer pointer by only 8 bytes per iteration, causing items after the | |
| first to be scrambled. A single :meth:`!byteswap` call produced incorrect | |
| results while a double call happened to round-trip back to the original | |
| value, which is why the existing test suite did not catch it. | |
| Fix :meth:`array.array.byteswap` corrupting data for ``'Zd'`` (complex | |
| double) arrays with more than one element: the 16-byte item loop advanced | |
| the buffer pointer by only 8 bytes per iteration, causing items after the | |
| first to be scrambled. |
Comment on lines
+1616
to
+1618
| # halves (real, imag) independently. The pre-existing test only | ||
| # checked that byteswap() twice round-trips to the original, | ||
| # which passes even if a single call scrambles multi-item arrays. |
Member
There was a problem hiding this comment.
Suggested change
| # halves (real, imag) independently. The pre-existing test only | |
| # checked that byteswap() twice round-trips to the original, | |
| # which passes even if a single call scrambles multi-item arrays. | |
| # halves (real, imag) independently. |
Comment on lines
+1619
to
+1626
| a = array.array(self.typecode, [1+2j, 3+4j, 5+6j]) | ||
| original = a.tobytes() | ||
| a.byteswap() | ||
| expected = bytearray() | ||
| for i in range(0, len(original), 16): | ||
| item = original[i:i + 16] | ||
| expected += item[7::-1] + item[15:7:-1] | ||
| self.assertEqual(a.tobytes(), bytes(expected)) |
Member
There was a problem hiding this comment.
I suggest to do this in a more generic way:
- Move to CFPTest
- use self.examples
- support all possible itemsize's
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.
array.array('Zd', ...).byteswap()scrambled every item after the firstbecause the 16-byte-item loop advanced the pointer by 8 bytes instead of
16. Fixes gh-154566.
Adds a regression test that checks the actual byte-level result of a
single
byteswap()call (the existing test only checked a double-callround-trip, which passes even under the bug since the corruption happens
to be self-canceling on the second call).