Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a2d9d7

Browse files
committed
GNUTranslations._parse(): Fix portability problems on 64-bit machines
by masking all unsigned integers with 0xffffffff.
1 parent 0c4fdba commit 9a2d9d7

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

Lib/gettext.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ class GNUTranslations(NullTranslations):
129129

130130
def _parse(self, fp):
131131
"""Override this method to support alternative .mo formats."""
132+
# We need to & all 32 bit unsigned integers with 0xffffff for
133+
# portability to 64 bit machines.
134+
MASK = 0xffffffff
132135
unpack = struct.unpack
133136
filename = getattr(fp, 'name', '')
134137
# Parse the .mo file header, which consists of 5 little endian 32
135138
# bit words.
136139
self._catalog = catalog = {}
137140
buf = fp.read()
141+
buflen = len(buf)
138142
# Are we big endian or little endian?
139-
magic = unpack('<i', buf[:4])[0]
143+
magic = unpack('<i', buf[:4])[0] & MASK
140144
if magic == self.LE_MAGIC:
141145
version, msgcount, masteridx, transidx = unpack('<4i', buf[4:20])
142146
ii = '<ii'
@@ -145,15 +149,20 @@ def _parse(self, fp):
145149
ii = '>ii'
146150
else:
147151
raise IOError(0, 'Bad magic number', filename)
148-
#
152+
# more unsigned ints
153+
msgcount &= MASK
154+
masteridx &= MASK
155+
transidx &= MASK
149156
# Now put all messages from the .mo file buffer into the catalog
150157
# dictionary.
151158
for i in xrange(0, msgcount):
152159
mlen, moff = unpack(ii, buf[masteridx:masteridx+8])
153-
mend = moff + mlen
160+
moff &= MASK
161+
mend = moff + (mlen & MASK)
154162
tlen, toff = unpack(ii, buf[transidx:transidx+8])
155-
tend = toff + tlen
156-
if mend < len(buf) and tend < len(buf):
163+
toff &= MASK
164+
tend = toff + (tlen & MASK)
165+
if mend < buflen and tend < buflen:
157166
tmsg = buf[toff:tend]
158167
catalog[buf[moff:mend]] = tmsg
159168
else:

0 commit comments

Comments
 (0)