Skip to content

gh-153605: Fix gettext.GNUTranslations struct.error on a malformed .mo file - #153606

Merged
StanFromIreland merged 4 commits into
python:mainfrom
tonghuaroot:gettext-mo-struct-error
Oct 25, 2026
Merged

StanFromIreland merged 4 commits into
python:mainfrom
tonghuaroot:gettext-mo-struct-error

Conversation

@tonghuaroot

Copy link
Copy Markdown
Contributor

gettext.GNUTranslations unpacked the .mo header and per-entry seek tables
with struct.unpack without first checking the file was long enough, so a
truncated file or one whose seek tables point past the end raised a raw
struct.error instead of the OSError the module already raises for other
malformed input. Validate the lengths and raise OSError instead.

…med .mo file

GNUTranslations._parse unpacked the .mo header and seek tables without
checking the file was long enough, so a truncated file or one whose seek
tables point past the end leaked a struct.error instead of the OSError the
module raises for other malformed input. Validate the lengths first.
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 12, 2026
Comment thread Lib/gettext.py Outdated
hdr = '>4I'
else:
raise OSError(0, 'Bad magic number', filename)
if buflen < 20:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this dance is worth it for better error messages here, I suggest we move the check above and just loose the "Bad magic number" error. They are only raised on truncated files, which are unlikely in practice.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed the change.

@StanFromIreland
StanFromIreland enabled auto-merge (squash) October 25, 2026 17:34
@StanFromIreland
StanFromIreland merged commit bb7e16a into python:main Oct 25, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants