From dc7d016538bb8ae67f2521ae07d1e333454c99dc Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Tue, 16 Jun 2020 16:35:40 +0200 Subject: [PATCH] [ttLib.tables._n_a_m_e] Fix #1997: Only attempt to recovered malformed data from bytes (#1998) * don't attempt to recover malformed data from str, only from bytes. Fixes #1997 --- Lib/fontTools/ttLib/tables/_n_a_m_e.py | 2 +- Tests/ttLib/tables/_n_a_m_e_test.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/ttLib/tables/_n_a_m_e.py b/Lib/fontTools/ttLib/tables/_n_a_m_e.py index a973fe3c9..4bb9024ba 100644 --- a/Lib/fontTools/ttLib/tables/_n_a_m_e.py +++ b/Lib/fontTools/ttLib/tables/_n_a_m_e.py @@ -441,7 +441,7 @@ class NameRecord(object): 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, diff --git a/Tests/ttLib/tables/_n_a_m_e_test.py b/Tests/ttLib/tables/_n_a_m_e_test.py index a12c1ade5..bc4aab2f1 100644 --- a/Tests/ttLib/tables/_n_a_m_e_test.py +++ b/Tests/ttLib/tables/_n_a_m_e_test.py @@ -345,6 +345,11 @@ class NameRecordTest(unittest.TestCase): 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)