Ignore invalid utf-8 strings (closes #49)

This commit is contained in:
Lonami Exo 2017-05-02 16:16:36 +02:00
parent 6069188b54
commit 3d66062eb4

View File

@ -89,7 +89,19 @@ class BinaryReader:
def tgread_string(self):
"""Reads a Telegram-encoded string"""
return str(self.tgread_bytes(), encoding='utf-8')
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)
def tgread_bool(self):
"""Reads a Telegram boolean value"""