Catch an error unpack might throw instead of checking the length before

pull/96/head
Sebastian Schuberth 2017-10-18 17:49:42 +02:00
parent ec8530f6cf
commit fcd3e6ce00
1 changed files with 10 additions and 10 deletions

View File

@ -383,16 +383,16 @@ class TrueTypeFont(object):
self.fp = fp self.fp = fp
self.tables = {} self.tables = {}
self.fonttype = fp.read(4) self.fonttype = fp.read(4)
data_str = fp.read(8) try:
if len(data_str) != 8: (ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
return
(ntables, _1, _2, _3) = struct.unpack('>HHHH', data_str)
for _ in range(ntables): for _ in range(ntables):
data_str = fp.read(16) (name, tsum, offset, length) = struct.unpack('>4sLLL', fp.read(16))
if len(data_str) != 16:
return
(name, tsum, offset, length) = struct.unpack('>4sLLL', data_str)
self.tables[name] = (offset, length) self.tables[name] = (offset, length)
except struct.error:
# Do not fail if there are not enough bytes to read. Even for
# corrupted PDFs we would like to get as much information as
# possible, so continue.
pass
return return
def create_unicode_map(self): def create_unicode_map(self):