Skip to content

Commit afe2378

Browse files
committed
Fix #15283 (donate-cpu: the assertion failure message is not saved when Cppcheck crashes)
1 parent 7578129 commit afe2378

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,14 @@ def test_match_multiple_time(tmpdir):
6666
# there was a bug that we would only match each library once successfully
6767
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
6868
_test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include <zlib.h>', libinc)
69+
70+
def test_get_assertion_failures():
71+
assert get_assertion_failures('') == []
72+
assert get_assertion_failures('Checking file.cpp ...\n') == []
73+
glibc_msg = "cppcheck: build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed."
74+
assert get_assertion_failures('Checking file.cpp ...\n' + glibc_msg + '\n') == [glibc_msg]
75+
# same message twice => reported once
76+
assert get_assertion_failures(glibc_msg + '\n' + glibc_msg + '\n') == [glibc_msg]
77+
# not an assertion failure message
78+
assert get_assertion_failures("Assertion `x' failed.\n") == []
79+
assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == []

‎tools/donate_cpu_lib.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/
1717
# Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic
1818
# changes)
19-
CLIENT_VERSION = "1.4.1"
19+
CLIENT_VERSION = "1.4.2"
2020

2121
# Timeout for analysis with Cppcheck in seconds
2222
CPPCHECK_TIMEOUT = 30 * 60
@@ -437,6 +437,20 @@ def __run_command(cmd, print_cmd=True):
437437
return return_code, stdout, stderr, elapsed_time
438438

439439

440+
# glibc assertion failure message: "<program>: <file>:<line>: <function>: Assertion `<expression>' failed."
441+
# Example: cppcheck: lib/token.cpp:123: void Token::foo(): Assertion `x' failed.
442+
__assertion_failure_re = re.compile(r"^\S+: [^:\s]+:\d+: .+: Assertion `.+' failed\.$")
443+
444+
445+
def get_assertion_failures(text):
446+
messages = []
447+
for line in text.split('\n'):
448+
line = line.strip()
449+
if __assertion_failure_re.match(line) and line not in messages:
450+
messages.append(line)
451+
return messages
452+
453+
440454
def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, enable='style,information', debug_warnings=True, check_level=None, extra_args=None):
441455
print('Analyze..')
442456
libs = ''
@@ -536,6 +550,7 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
536550
# make sure we have the actual error code set
537551
if has_sig:
538552
returncode = -sig_num
553+
assertion_failures = get_assertion_failures(stderr)
539554
stacktrace = ''
540555
if capture_callstack:
541556
# re-run within gdb to get a stacktrace
@@ -544,20 +559,33 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
544559
cmd += sig_file
545560
else:
546561
cmd += dir_to_scan
547-
_, st_stdout, _, _ = __run_command(cmd)
562+
_, st_stdout, st_stderr, _ = __run_command(cmd)
563+
for msg in get_assertion_failures(st_stderr):
564+
if msg not in assertion_failures:
565+
assertion_failures.append(msg)
548566
gdb_pos = st_stdout.find(" received signal")
549567
if gdb_pos != -1:
550568
last_check_pos = st_stdout.rfind('Checking ', 0, gdb_pos)
551569
if last_check_pos == -1:
552570
stacktrace = st_stdout[gdb_pos:]
553571
else:
554-
stacktrace = st_stdout[last_check_pos:]
572+
# put the assertion failure messages before the ' received signal ' line, the servers parse the stack trace after that line
573+
signal_line_pos = st_stdout.rfind('\n', 0, gdb_pos) + 1
574+
if signal_line_pos < last_check_pos:
575+
signal_line_pos = last_check_pos
576+
stacktrace = st_stdout[last_check_pos:signal_line_pos] + \
577+
''.join(msg + '\n' for msg in assertion_failures) + \
578+
st_stdout[signal_line_pos:]
579+
assertion_failures = []
555580
# if no stacktrace was generated return the original stdout or internal errors list
556581
if not stacktrace:
557582
if has_sig:
558583
stacktrace = ''.join(internal_error_messages_list)
559584
else:
560585
stacktrace = stdout
586+
# assertion failure messages that were not put in the gdb output
587+
if assertion_failures:
588+
stacktrace = ''.join(msg + '\n' for msg in assertion_failures) + stacktrace
561589
return returncode, stacktrace, '', returncode, options_j, ''
562590

563591
if returncode != 0:

0 commit comments

Comments
 (0)