Skip to content

Commit 5d47204

Browse files
tomasr8miss-islington
authored andcommitted
pythongh-130655: Add a test for corrupt .mo files in gettext (pythonGH-131911)
(cherry picked from commit a126cef) Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
1 parent 701ac74 commit 5d47204

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Lib/test/test_gettext.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,32 @@
8787
ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
8888
'''
8989

90+
# Corrupt .mo file
91+
# Generated from
92+
#
93+
# msgid "foo"
94+
# msgstr "bar"
95+
#
96+
# with msgfmt --no-hash
97+
#
98+
# The translation offset is changed to 0xFFFFFFFF,
99+
# making it larger than the file size, which should
100+
# raise an error when parsing.
101+
GNU_MO_DATA_CORRUPT = base64.b64encode(bytes([
102+
0xDE, 0x12, 0x04, 0x95, # Magic
103+
0x00, 0x00, 0x00, 0x00, # Version
104+
0x01, 0x00, 0x00, 0x00, # Message count
105+
0x1C, 0x00, 0x00, 0x00, # Message offset
106+
0x24, 0x00, 0x00, 0x00, # Translation offset
107+
0x00, 0x00, 0x00, 0x00, # Hash table size
108+
0x2C, 0x00, 0x00, 0x00, # Hash table offset
109+
0x03, 0x00, 0x00, 0x00, # 1st message length
110+
0x2C, 0x00, 0x00, 0x00, # 1st message offset
111+
0x03, 0x00, 0x00, 0x00, # 1st trans length
112+
0xFF, 0xFF, 0xFF, 0xFF, # 1st trans offset (Modified to make it invalid)
113+
0x66, 0x6F, 0x6F, 0x00, # Message data
114+
0x62, 0x61, 0x72, 0x00, # Message data
115+
]))
90116

91117
UMO_DATA = b'''\
92118
3hIElQAAAAADAAAAHAAAADQAAAAAAAAAAAAAAAAAAABMAAAABAAAAE0AAAAQAAAAUgAAAA8BAABj
@@ -113,6 +139,7 @@
113139
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
114140
MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
115141
MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
142+
MOFILE_CORRUPT = os.path.join(LOCALEDIR, 'gettext_corrupt.mo')
116143
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
117144
MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
118145

@@ -135,6 +162,8 @@ def setUpClass(cls):
135162
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
136163
with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
137164
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MINOR_VERSION))
165+
with open(MOFILE_CORRUPT, 'wb') as fp:
166+
fp.write(base64.decodebytes(GNU_MO_DATA_CORRUPT))
138167
with open(UMOFILE, 'wb') as fp:
139168
fp.write(base64.decodebytes(UMO_DATA))
140169
with open(MMOFILE, 'wb') as fp:
@@ -264,6 +293,16 @@ def test_bad_minor_version(self):
264293
# Check that no error is thrown with a bad minor version number
265294
gettext.GNUTranslations(fp)
266295

296+
def test_corrupt_file(self):
297+
with open(MOFILE_CORRUPT, 'rb') as fp:
298+
with self.assertRaises(OSError) as cm:
299+
gettext.GNUTranslations(fp)
300+
301+
exception = cm.exception
302+
self.assertEqual(exception.errno, 0)
303+
self.assertEqual(exception.strerror, "File is corrupt")
304+
self.assertEqual(exception.filename, MOFILE_CORRUPT)
305+
267306
def test_some_translations(self):
268307
eq = self.assertEqual
269308
# test some translations

0 commit comments

Comments
 (0)