Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Lib/gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,31 @@ def _parse(self, fp):
buf = fp.read()
buflen = len(buf)
# Are we big endian or little endian?
if buflen < 4:
raise OSError(0, 'File is corrupt', filename)
magic = unpack('<I', buf[:4])[0]
if magic == self.LE_MAGIC:
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20])
ii = '<II'
hdr = '<4I'
elif magic == self.BE_MAGIC:
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20])
ii = '>II'
hdr = '>4I'
else:
raise OSError(0, 'Bad magic number', filename)
if buflen < 20:
raise OSError(0, 'File is corrupt', filename)
version, msgcount, masteridx, transidx = unpack(hdr, buf[4:20])

major_version, minor_version = self._get_versions(version)

if major_version not in self.VERSIONS:
raise OSError(0, 'Bad version number ' + str(major_version), filename)

# The seek tables must lie within the file.
if (masteridx + 8 * msgcount > buflen
or transidx + 8 * msgcount > buflen):
raise OSError(0, 'File is corrupt', filename)

# Now put all messages from the .mo file buffer into the catalog
# dictionary.
for i in range(0, msgcount):
Expand Down
34 changes: 34 additions & 0 deletions Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import base64
import gettext
import io
import unittest
import unittest.mock
from functools import partial
Expand Down Expand Up @@ -314,6 +315,39 @@ def test_corrupt_file(self):
self.assertEqual(exception.strerror, "File is corrupt")
self.assertEqual(exception.filename, MOFILE_CORRUPT)

def test_truncated_header(self):
magic = b'\xde\x12\x04\x95'
for buf in (b'', magic, magic + bytes(15)):
with self.subTest(buflen=len(buf)):
with self.assertRaises(OSError) as cm:
gettext.GNUTranslations(io.BytesIO(buf))
self.assertEqual(cm.exception.errno, 0)
self.assertEqual(cm.exception.strerror, "File is corrupt")

def test_offsets_out_of_bounds(self):
buf = bytes([
0xde, 0x12, 0x04, 0x95, # Magic
0x00, 0x00, 0x00, 0x00, # Version
0x01, 0x00, 0x00, 0x00, # Message count
0xe8, 0x03, 0x00, 0x00, # Message offset (past EOF)
0xd0, 0x07, 0x00, 0x00, # Translation offset (past EOF)
])
with self.assertRaises(OSError) as cm:
gettext.GNUTranslations(io.BytesIO(buf))
self.assertEqual(cm.exception.errno, 0)
self.assertEqual(cm.exception.strerror, "File is corrupt")

def test_empty_catalog(self):
buf = bytes([
0xde, 0x12, 0x04, 0x95, # Magic
0x00, 0x00, 0x00, 0x00, # Version
0x00, 0x00, 0x00, 0x00, # Message count
0x14, 0x00, 0x00, 0x00, # Message offset
0x14, 0x00, 0x00, 0x00, # Translation offset
])
t = gettext.GNUTranslations(io.BytesIO(buf))
self.assertEqual(t.gettext('foo'), 'foo')

def test_big_endian_file(self):
with open(MOFILE_BIG_ENDIAN, 'rb') as fp:
t = gettext.GNUTranslations(fp)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`gettext.GNUTranslations` raising :exc:`struct.error` instead of
:exc:`OSError` when reading a malformed ``.mo`` file. Patch by tonghuaroot.
Loading