Skip to content

Commit

Permalink
Fix crashes with non-UTF8 logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chih-Hsuan Yen committed May 27, 2021
1 parent b775609 commit 797bf57
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion logcatcolor/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def set_file(self, fd):
fcntl.fcntl(fd, fcntl.F_SETFL, flags)

def collect_incoming_data(self, data):
self.log_buffer.write(data.decode('utf-8'))
# some logcat message may not be valid UTF-8. For example, Magisk Manager after
# hiding uses title Manager\xc0\x80\xc0\x80\xc0\x80\xc0\x80\xc0\x80\xc0\x80\xc0\x80
self.log_buffer.write(data.decode('utf-8', errors='backslashreplace'))

def found_terminator(self):
line = self.log_buffer.getvalue()
Expand Down
1 change: 1 addition & 0 deletions test/logs/non_utf8_log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I/Tag( 123): message��
1 change: 1 addition & 0 deletions test/logs/non_utf8_output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I/Tag( 123): message\xc0\x80
9 changes: 9 additions & 0 deletions test/test_logcat_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def wrapped(self):
configs_dir = os.path.join(this_dir, "configs")

BRIEF_LOG = os.path.join(logs_dir, "brief_log")
NON_UTF8_LOG = os.path.join(logs_dir, "non_utf8_log")
NON_UTF8_OUTPUT = os.path.join(logs_dir, "non_utf8_output")
BRIEF_FILTER_CONFIG = os.path.join(configs_dir, "brief_filter_config")
EMPTY_CONFIG = os.path.join(configs_dir, "empty_config")

Expand Down Expand Up @@ -142,6 +144,13 @@ def test_file_output(self):
out_data = f.read()
self.assertEqual(out_data, brief_data)

@logcat_color_test("--plain", input=NON_UTF8_LOG)
def test_non_utf8_output(self):
self.assertEqual(self.proc.returncode, 0)
with open(NON_UTF8_OUTPUT, "rt") as f:
non_utf8_output = f.read()
self.assertEqual(self.out, non_utf8_output)

def test_logcat_options_with_filters(self):
# Make sure logcat flags come before filter arguments
# https://github.com/marshall/logcat-color/issues/5
Expand Down

0 comments on commit 797bf57

Please sign in to comment.