File 0003-fix-hb_report-handle-UnicodeDecodeError-bsc-1130715.patch of Package crmsh.12576
From c64fd162f4ab6e3ad48f26c27b6662e020a89c01 Mon Sep 17 00:00:00 2001
From: liangxin1300 <XLiang@suse.com>
Date: Mon, 8 Apr 2019 13:50:28 +0800
Subject: [PATCH 3/4] fix: hb_report: handle UnicodeDecodeError(bsc#1130715)
* setting error='replace' to replace invalid utf-8 characters * try to
catch UnicodeDecodeError and print traceback
---
hb_report/hb_report.in | 7 ++++++-
hb_report/utillib.py | 10 +++++-----
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/hb_report/hb_report.in b/hb_report/hb_report.in
index 40de4047..49138668 100755
--- a/hb_report/hb_report.in
+++ b/hb_report/hb_report.in
@@ -409,6 +409,11 @@ def version():
if __name__ == "__main__":
- run()
+ try:
+ run()
+ except UnicodeDecodeError:
+ import traceback
+ traceback.print_exc()
+ sys.stdout.flush()
# vim:ts=4:sw=4:et:
diff --git a/hb_report/utillib.py b/hb_report/utillib.py
index 57ca0dc6..5467fd00 100644
--- a/hb_report/utillib.py
+++ b/hb_report/utillib.py
@@ -594,10 +594,10 @@ def find_first_ts(data):
def filter_lines(logf, from_line, to_line=None):
out_string = ""
if not to_line:
- to_line = sum(1 for l in open(logf, 'r', encoding='utf-8'))
+ to_line = sum(1 for l in open(logf, 'r', encoding='utf-8', errors='replace'))
count = 1
- with open(logf, 'r', encoding='utf-8') as f:
+ with open(logf, 'r', encoding='utf-8', errors='replace') as f:
for line in f.readlines():
if count >= from_line and count <= to_line:
out_string += line
@@ -625,7 +625,7 @@ def finalword():
def find_getstampproc(log_file):
func = None
loop_cout = 10
- with open(log_file, 'r', encoding='utf-8') as f:
+ with open(log_file, 'r', encoding='utf-8', errors='replace') as f:
for line in f.readlines():
if loop_cout == 0:
break
@@ -720,7 +720,7 @@ def find_ssh_user():
def findln_by_time(logf, tm):
tmid = None
first = 1
- last = sum(1 for l in open(logf, 'r', encoding='utf-8'))
+ last = sum(1 for l in open(logf, 'r', encoding='utf-8', errors='replace'))
while first <= last:
mid = (last+first)//2
trycnt = 10
@@ -1200,7 +1200,7 @@ def is_our_log(logf, from_time, to_time):
def line_time(logf, line_num):
ts = None
- with open(logf, 'r', encoding='utf-8') as fd:
+ with open(logf, 'r', encoding='utf-8', errors='replace') as fd:
line_res = head(line_num, fd.read())
if line_res:
ts = get_ts(line_res[-1])
--
2.22.1