Skip to content

ZoneInfo.from_file() accepts any TZif charcnt and can use 512x the file size in memory #161096

Closed

Description

@gpshead

Bug report

Bug description:

ZoneInfo.from_file() takes the TZif header's charcnt on trust: a negative value makes the abbreviation block the rest of the file, and since each distinct abbreviation index gets its own copy of the bytes up to the next NUL, one long block referenced by 128 local time types leaves the ZoneInfo holding about 512 times the file size. abbrind is also unpacked as a signed byte, so indexes 128 to 255 count from the end of the block and give the wrong abbreviation.

import io, struct
from zoneinfo import ZoneInfo

block = b"A" * (2**20 - 5) + "\U0001F552".encode() + b"\0"
types = b"".join(struct.pack(">lbb", 3600, 0, i) for i in range(128))
head = b"TZif" + bytes(16) + struct.pack(">6l", 0, 0, 0, 0, 128, len(block))
zone = ZoneInfo.from_file(io.BytesIO(head + types + block))  # ~537 MB held for a 1 MiB file

Expected ValueError, as for other malformed TZif data; tzcode's zic caps the block at 50 bytes and no installed zone file comes close. Related: gh-143241 and gh-145883.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 1, 2026
  3. added a commit that references this issue on Nov 1, 2026
  4. added 2 commits that reference this issue on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions