From fcd3e6ce00d828b1f3c71a756865fea41dc9d4bf Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 18 Oct 2017 17:49:42 +0200 Subject: [PATCH] Catch an error unpack might throw instead of checking the length before --- pdfminer/pdffont.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pdfminer/pdffont.py b/pdfminer/pdffont.py index 99f7811..feb8557 100644 --- a/pdfminer/pdffont.py +++ b/pdfminer/pdffont.py @@ -383,16 +383,16 @@ class TrueTypeFont(object): self.fp = fp self.tables = {} self.fonttype = fp.read(4) - data_str = fp.read(8) - if len(data_str) != 8: - return - (ntables, _1, _2, _3) = struct.unpack('>HHHH', data_str) - for _ in range(ntables): - data_str = fp.read(16) - if len(data_str) != 16: - return - (name, tsum, offset, length) = struct.unpack('>4sLLL', data_str) - self.tables[name] = (offset, length) + try: + (ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8)) + for _ in range(ntables): + (name, tsum, offset, length) = struct.unpack('>4sLLL', fp.read(16)) + 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 def create_unicode_map(self):