Skip to content

struct.pack() doesn't checks overflows for native 'Zf' format type #156867

Description

@skirpichev

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 format

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions