From 8ffec6b8be965d6eac048c27af7d13e5bcb25116 Mon Sep 17 00:00:00 2001 From: Gholamhossein Tavasoli Date: Wed, 3 May 2017 15:32:44 +0430 Subject: [PATCH] Improve handling invalid UTF-8 strings (#68) --- telethon/utils/binary_reader.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/telethon/utils/binary_reader.py b/telethon/utils/binary_reader.py index a13c9a0f..04103ae7 100755 --- a/telethon/utils/binary_reader.py +++ b/telethon/utils/binary_reader.py @@ -89,19 +89,7 @@ class BinaryReader: def tgread_string(self): """Reads a Telegram-encoded string""" - return self._decode_string(self.tgread_bytes()) - - @staticmethod - def _decode_string(encoded): - # Workaround for issues #49 and #67 - # Sometimes an invalid utf-8 string is received. We can just remove - # the offending parts and replace them with a different solution. - # There aren't many other options, besides crashing. - try: - return str(encoded, encoding='utf-8') - except UnicodeDecodeError as e: - fixed = encoded[:e.start] + encoded[e.end:] - return BinaryReader._decode_string(fixed) + return str(self.tgread_bytes(), encoding='utf-8', errors='replace') def tgread_bool(self): """Reads a Telegram boolean value"""