Skip to content

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

Closed as duplicate

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

Activity

  1. self-assigned this
    on Sep 3, 2026
  2. removed their assignment
    on Sep 3, 2026
  3. vstinner commented on Sep 3, 2026

    @vstinner
    Member

    This issue looks similar to issue gh-156864. I would prefer to merge the two issues and fix all issues at once.

  4. self-assigned this
    on Sep 3, 2026
  5. added
    pendingThe issue will be closed if no feedback is provided
    and removed
    pendingThe issue will be closed if no feedback is provided
    on Sep 3, 2026
  6. removed their assignment
    on Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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