Skip to content

gettext.GNUTranslations raises struct.error instead of OSError on a malformed .mo file #153605

Description

@tonghuaroot

Bug report

Bug description:

gettext.GNUTranslations parses a .mo file by unpacking its header and seek
tables with struct.unpack, but never checks the file is long enough. A
truncated file (shorter than the 20-byte header) or one whose seek tables point
past the end raises a raw struct.error instead of the OSError the module
already raises for other malformed input (a bad magic number, a bad version, or
an out-of-range string offset). The documentation states OSError is raised for
problems while reading the file, so the struct.error is an undocumented leak.

import gettext, io
gettext.GNUTranslations(io.BytesIO(b"\xde\x12\x04\x95")) # raises struct.error

CPython versions tested on:

3.13, 3.14, 3.15, 3.16

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions