struct.pack() doesn't checks overflows for native 'Zf' format type #156867
Copy link
Copy link
Closed as duplicate
Closed as duplicate
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
This is similar to that issue with 'f' format type.
Consider:
>>> import struct
>>> struct.pack('f', 1e300)
Traceback (most recent call last):
File "<python-input-10>", line 1, in <module>
struct.pack('f', 1e300)
~~~~~~~~~~~^^^^^^^^^^^^
OverflowError: float too large to pack with f format
>>> struct.pack('Zf', 1e300)
b'\x00\x00\x80\x7f\x00\x00\x00\x00'
>>> struct.unpack('Zf', _)
((inf+0j),)
>>> struct.pack('<Zf', 1e300) # le box, we fallback to memcpy()
b'\x00\x00\x80\x7f\x00\x00\x00\x00'
>>> struct.pack('>Zf', 1e300)
Traceback (most recent call last):
File "<python-input-14>", line 1, in <module>
struct.pack('>Zf', 1e300)
~~~~~~~~~~~^^^^^^^^^^^^^^
OverflowError: float too large to pack with f formatCPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
vstinner commentedon Sep 3, 2026
This issue looks similar to issue gh-156864. I would prefer to merge the two issues and fix all issues at once.