Skip to content

Commit

Permalink
[ttLib.tables._n_a_m_e] Fix #1997: Only attempt to recovered malforme…
Browse files Browse the repository at this point in the history
…d data from bytes (#1998)

* don't attempt to recover malformed data from str, only from bytes. Fixes #1997
  • Loading branch information
justvanrossum committed Jun 16, 2020
1 parent 6b4430a commit dc7d016
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/fontTools/ttLib/tables/_n_a_m_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def isascii(b):
encoding = self.getEncoding()
string = self.string

if encoding == 'utf_16_be' and len(string) % 2 == 1:
if isinstance(string, bytes) and encoding == 'utf_16_be' and len(string) % 2 == 1:
# Recover badly encoded UTF-16 strings that have an odd number of bytes:
# - If the last byte is zero, drop it. Otherwise,
# - If all the odd bytes are zero and all the even bytes are ASCII,
Expand Down
5 changes: 5 additions & 0 deletions Tests/ttLib/tables/_n_a_m_e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ def test_toUnicode_UnicodeDecodeError(self):
self.assertEqual("utf_16_be", name.getEncoding())
self.assertRaises(UnicodeDecodeError, name.toUnicode)

def test_toUnicode_singleChar(self):
# https://github.com/fonttools/fonttools/issues/1997
name = makeName("A", 256, 3, 1, 0x409)
self.assertEqual(name.toUnicode(), "A")

def toXML(self, name):
writer = XMLWriter(BytesIO())
name.toXML(writer, ttFont=None)
Expand Down

0 comments on commit dc7d016

Please sign in to comment.