From 46a0ab17547fd4c4e9b8731797498fc22c9711eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Mon, 21 Jul 2014 01:28:29 +0200 Subject: [PATCH] htmlreport: fix #5998, crash on some files with "UnicodeDecodeError". For those files that we can't decode, don't generate a html-preview but note into index.html that we couldn't process the file. At the end, summarize on which files we failed and suggest using --source-encoding option. --- htmlreport/cppcheck-htmlreport | 57 +++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/htmlreport/cppcheck-htmlreport b/htmlreport/cppcheck-htmlreport index fef059d0f..872544035 100755 --- a/htmlreport/cppcheck-htmlreport +++ b/htmlreport/cppcheck-htmlreport @@ -308,6 +308,7 @@ if __name__ == '__main__': # Generate a HTML file with syntax highlighted source code for each # file that contains one or more errors. print('Processing errors') + decode_errors = [] for filename, data in sorted(files.items()): htmlfile = data['htmlfile'] errors = data['errors'] @@ -327,6 +328,11 @@ if __name__ == '__main__': sys.stderr.write("ERROR: Source file '%s' not found.\n" % source_filename) continue + except UnicodeDecodeError: + sys.stderr.write("WARNING: Unicode decode error in '%s'.\n" % + source_filename) + decode_errors.append(source_filename[2:]) # "[2:]" gets rid of "./" at beginning + continue htmlFormatter = AnnotateCodeFormatter(linenos=True, style='colorful', @@ -369,33 +375,42 @@ if __name__ == '__main__': output_file.write( ' LineIdSeverityMessage') for filename, data in sorted(files.items()): - output_file.write( + if filename in decode_errors: # don't print a link but a note + output_file.write("\n %s" % (filename)) + output_file.write("\n Could not generated due to UnicodeDecodeError") + else: + output_file.write( "\n %s" % (data['htmlfile'], filename)) - for error in data['errors']: - error_class = '' - try: - if error['inconclusive'] == 'true': - error_class = 'class="inconclusive"' - error['severity'] += ", inconcl." - except KeyError: - pass + for error in data['errors']: + error_class = '' + try: + if error['inconclusive'] == 'true': + error_class = 'class="inconclusive"' + error['severity'] += ", inconcl." + except KeyError: + pass + + if error['severity'] == 'error': + error_class = 'class="error"' + if error['id'] == 'missingInclude': + output_file.write( + '\n %s%s%s' % + (error['id'], error['severity'], error['msg'])) + else: + output_file.write( + "\n %d%s%s%s" % + (data['htmlfile'], error['line'], error['line'], + error['id'], error['severity'], error_class, + error['msg'])) - if error['severity'] == 'error': - error_class = 'class="error"' - if error['id'] == 'missingInclude': - output_file.write( - '\n %s%s%s' % - (error['id'], error['severity'], error['msg'])) - else: - output_file.write( - "\n %d%s%s%s" % - (data['htmlfile'], error['line'], error['line'], - error['id'], error['severity'], error_class, - error['msg'])) output_file.write('\n ') output_file.write(HTML_FOOTER % contentHandler.versionCppcheck) + if (decode_errors): + sys.stderr.write("\nGenerating html failed for the following files: " + ' '.join(decode_errors)) + sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n") + print('Creating style.css file') with io.open(os.path.join(options.report_dir, 'style.css'), 'w') as css_file: