Skip to content

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

Closed

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jul 12, 2026
  2. serhiy-storchaka commented on Aug 17, 2026

    @serhiy-storchaka
    Member

    Since OSError is not specific to gettext, other option -- simply document other exceptions.

  3. added a commit that references this issue on Oct 25, 2026
  4. StanFromIreland commented on Oct 25, 2026

    @StanFromIreland
    Member

    Since OSError is not specific to gettext, other option -- simply document other exceptions.

    That's also an option, but I think the error message may currently be confusing for users (it looks like a bug in gettext rather than your catalogue!), so I merged a simplified patch.

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

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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions