Skip to content

Commit

Permalink
Merge pull request #3684 from qstokkink/fix_tdef_name
Browse files Browse the repository at this point in the history
Fixed crash on torrents with exotic names
  • Loading branch information
xoriole authored Jun 25, 2018
2 parents 55d2d99 + 403bc45 commit 39b2132
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Tribler/Core/Modules/restapi/downloads_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def render_GET(self, request):

num_seeds, num_peers = state.get_num_seeds_peers()

download_json = {"name": tdef.get_name(), "progress": state.get_progress(),
download_json = {"name": tdef.get_name_utf8(), "progress": state.get_progress(),
"infohash": tdef.get_infohash().encode('hex'),
"speed_down": state.get_current_speed(DOWNLOAD),
"speed_up": state.get_current_speed(UPLOAD),
Expand Down
18 changes: 18 additions & 0 deletions Tribler/Core/TorrentDef.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,24 @@ def get_name(self):
else:
raise TorrentDefNotFinalizedException()

def get_name_utf8(self):
"""
Not all names are utf-8, attempt to construct it as utf-8 anyway.
"""
out = self.get_name()
try:
# Try seeing if the delivered encoding is correct and we
# can convert to utf8 without any issues.
return out.decode(self.get_encoding()).encode('utf8').decode('utf8')
except (LookupError, TypeError, ValueError):
try:
# The delivered encoding is incorrect, cast it to
# latin1 and hope for the best (minor corruption).
return out.decode('latin1').encode('utf8', 'ignore').decode('utf8')
except (TypeError, ValueError):
# This is a very nasty string (e.g. u'\u266b'), remove the illegal entries.
return out.encode('utf8', 'ignore').decode('utf8')

def set_name(self, name):
""" Set the name of this torrent
@param name name of torrent as String
Expand Down
19 changes: 19 additions & 0 deletions Tribler/Test/Core/test_torrent_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ def test_add_content_dir_and_file(self):
reals += file['length']
self.assertEqual(exps, reals)

def test_get_name_utf8(self):
""" Add a TorrentDef with non-utf8 encoding"""
t = TorrentDef()
t.set_name('\xA1\xC0')
t.set_encoding('euc_kr')
t.set_tracker(TRACKER)
t.finalize()

self.assertEqual(t.get_name_utf8(), u'\xf7')

def test_get_name_utf8_unknown(self):
""" Add a TorrentDef with non-utf8 encoding"""
t = TorrentDef()
t.set_name('\xA1\xC0')
t.set_tracker(TRACKER)
t.finalize()

self.assertEqual(t.get_name_utf8(), u'\xa1\xc0')

def test_add_content_announce_list(self):
""" Add a single file with announce-list to a TorrentDef """
t = TorrentDef()
Expand Down

0 comments on commit 39b2132

Please sign in to comment.