Skip to content

Commit 16a6270

Browse files
authored
gh-130655: Add a test for bad magic numbers in .mo files parsed by gettext (#131909)
1 parent 891c61c commit 16a6270

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lib/test/test_gettext.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA
3838
'''
3939

40+
# .mo file with an invalid magic number
41+
GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD')
42+
4043
# This data contains an invalid major version number (5)
4144
# An unexpected major version number should be treated as an error when
4245
# parsing a .mo file
@@ -109,6 +112,7 @@
109112

110113
LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
111114
MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
115+
MOFILE_BAD_MAGIC_NUMBER = os.path.join(LOCALEDIR, 'gettext_bad_magic_number.mo')
112116
MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo')
113117
MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo')
114118
UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
@@ -129,6 +133,8 @@ def setUpClass(cls):
129133
os.makedirs(LOCALEDIR)
130134
with open(MOFILE, 'wb') as fp:
131135
fp.write(base64.decodebytes(GNU_MO_DATA))
136+
with open(MOFILE_BAD_MAGIC_NUMBER, 'wb') as fp:
137+
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAGIC_NUMBER))
132138
with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp:
133139
fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION))
134140
with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp:
@@ -223,6 +229,16 @@ def test_bindtextdomain(self):
223229
def test_textdomain(self):
224230
self.assertEqual(gettext.textdomain(), 'gettext')
225231

232+
def test_bad_magic_number(self):
233+
with open(MOFILE_BAD_MAGIC_NUMBER, 'rb') as fp:
234+
with self.assertRaises(OSError) as cm:
235+
gettext.GNUTranslations(fp)
236+
237+
exception = cm.exception
238+
self.assertEqual(exception.errno, 0)
239+
self.assertEqual(exception.strerror, "Bad magic number")
240+
self.assertEqual(exception.filename, MOFILE_BAD_MAGIC_NUMBER)
241+
226242
def test_bad_major_version(self):
227243
with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp:
228244
with self.assertRaises(OSError) as cm:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy