Skip to content

Commit

Permalink
Fix two crashes on slightly broken font files
Browse files Browse the repository at this point in the history
The zeros at the end are not needed by our implementation so don't
crash if there are too few.

Always pass an even number of bytes to unhexlify.
  • Loading branch information
jkseppan committed Jul 22, 2021
1 parent 338d494 commit 06845a4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/matplotlib/type1font.py
Expand Up @@ -24,6 +24,7 @@
import binascii
import enum
import itertools
import logging
import re
import struct

Expand All @@ -32,6 +33,7 @@
from matplotlib.cbook import _format_approx
from . import _api

_log = logging.getLogger(__name__)

# token types
_TokenType = enum.Enum('_TokenType',
Expand Down Expand Up @@ -129,13 +131,16 @@ def _split(self, data):
zeros -= 1
idx -= 1
if zeros:
raise RuntimeError('Insufficiently many zeros in Type 1 font')
# this may have been a problem on old implementations that
# used the zeros as necessary padding
_log.info('Insufficiently many zeros in Type 1 font')

# Convert encrypted part to binary (if we read a pfb file, we may end
# up converting binary to hexadecimal to binary again; but if we read
# a pfa file, this part is already in hex, and I am not quite sure if
# even the pfb format guarantees that it will be in binary).
binary = binascii.unhexlify(data[len1:idx+1])
idx1 = len1 + ((idx - len1 + 2) & ~1) # ensure an even number of bytes
binary = binascii.unhexlify(data[len1:idx1])

return data[:len1], binary, data[idx+1:]

Expand Down

0 comments on commit 06845a4

Please sign in to comment.