python / python/cpython

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

Open
#153,605 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-153606

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at gettext.GNUTranslations and reproduce the truncated and out-of-range .mo cases described with io.BytesIO. The work is done when malformed input raises OSError consistently instead of leaking struct.error, with regression coverage for the reported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
localization
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.