From afe23781d430c043d4fcce0dc94d12bddb077fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Nov 2026 13:37:32 +0100 Subject: [PATCH 1/4] Fix #15283 (donate-cpu: the assertion failure message is not saved when Cppcheck crashes) --- test/tools/donate_cpu_lib_test.py | 11 ++++++++++ tools/donate_cpu_lib.py | 34 ++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index 9c569276a19..98fb5f2b3c6 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -66,3 +66,14 @@ def test_match_multiple_time(tmpdir): # there was a bug that we would only match each library once successfully _test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include ', libinc) _test_library_includes(tmpdir, ['posix', 'gnu', 'bsd', 'zlib'], '#include ', libinc) + +def test_get_assertion_failures(): + assert get_assertion_failures('') == [] + assert get_assertion_failures('Checking file.cpp ...\n') == [] + glibc_msg = "cppcheck: build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed." + assert get_assertion_failures('Checking file.cpp ...\n' + glibc_msg + '\n') == [glibc_msg] + # same message twice => reported once + assert get_assertion_failures(glibc_msg + '\n' + glibc_msg + '\n') == [glibc_msg] + # not an assertion failure message + assert get_assertion_failures("Assertion `x' failed.\n") == [] + assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == [] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index ce61495f660..aacb03df3bc 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -16,7 +16,7 @@ # Version scheme (MAJOR.MINOR.PATCH) should orientate on "Semantic Versioning" https://semver.org/ # Every change in this script should result in increasing the version number accordingly (exceptions may be cosmetic # changes) -CLIENT_VERSION = "1.4.1" +CLIENT_VERSION = "1.4.2" # Timeout for analysis with Cppcheck in seconds CPPCHECK_TIMEOUT = 30 * 60 @@ -437,6 +437,20 @@ def __run_command(cmd, print_cmd=True): return return_code, stdout, stderr, elapsed_time +# glibc assertion failure message: ": :: : Assertion `' failed." +# Example: cppcheck: lib/token.cpp:123: void Token::foo(): Assertion `x' failed. +__assertion_failure_re = re.compile(r"^\S+: [^:\s]+:\d+: .+: Assertion `.+' failed\.$") + + +def get_assertion_failures(text): + messages = [] + for line in text.split('\n'): + line = line.strip() + if __assertion_failure_re.match(line) and line not in messages: + messages.append(line) + return messages + + def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, enable='style,information', debug_warnings=True, check_level=None, extra_args=None): print('Analyze..') libs = '' @@ -536,6 +550,7 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, # make sure we have the actual error code set if has_sig: returncode = -sig_num + assertion_failures = get_assertion_failures(stderr) stacktrace = '' if capture_callstack: # re-run within gdb to get a stacktrace @@ -544,20 +559,33 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, cmd += sig_file else: cmd += dir_to_scan - _, st_stdout, _, _ = __run_command(cmd) + _, st_stdout, st_stderr, _ = __run_command(cmd) + for msg in get_assertion_failures(st_stderr): + if msg not in assertion_failures: + assertion_failures.append(msg) gdb_pos = st_stdout.find(" received signal") if gdb_pos != -1: last_check_pos = st_stdout.rfind('Checking ', 0, gdb_pos) if last_check_pos == -1: stacktrace = st_stdout[gdb_pos:] else: - stacktrace = st_stdout[last_check_pos:] + # put the assertion failure messages before the ' received signal ' line, the servers parse the stack trace after that line + signal_line_pos = st_stdout.rfind('\n', 0, gdb_pos) + 1 + if signal_line_pos < last_check_pos: + signal_line_pos = last_check_pos + stacktrace = st_stdout[last_check_pos:signal_line_pos] + \ + ''.join(msg + '\n' for msg in assertion_failures) + \ + st_stdout[signal_line_pos:] + assertion_failures = [] # if no stacktrace was generated return the original stdout or internal errors list if not stacktrace: if has_sig: stacktrace = ''.join(internal_error_messages_list) else: stacktrace = stdout + # assertion failure messages that were not put in the gdb output + if assertion_failures: + stacktrace = ''.join(msg + '\n' for msg in assertion_failures) + stacktrace return returncode, stacktrace, '', returncode, options_j, '' if returncode != 0: From 3218a05bb59bf8e55ec3125478e5992acdcc43ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Nov 2026 14:50:01 +0100 Subject: [PATCH 2/4] donate-cpu: fix pylint warning consider-using-max-builtin Co-Authored-By: Claude Opus 5.5 --- tools/donate_cpu_lib.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index aacb03df3bc..68bf5b39e2e 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -570,9 +570,7 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, stacktrace = st_stdout[gdb_pos:] else: # put the assertion failure messages before the ' received signal ' line, the servers parse the stack trace after that line - signal_line_pos = st_stdout.rfind('\n', 0, gdb_pos) + 1 - if signal_line_pos < last_check_pos: - signal_line_pos = last_check_pos + signal_line_pos = max(st_stdout.rfind('\n', 0, gdb_pos) + 1, last_check_pos) stacktrace = st_stdout[last_check_pos:signal_line_pos] + \ ''.join(msg + '\n' for msg in assertion_failures) + \ st_stdout[signal_line_pos:] From f6aaa820b0c5810a5f53edfcade56ec83d3985f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Nov 2026 17:50:22 +0100 Subject: [PATCH 3/4] review --- test/tools/donate_cpu_lib_test.py | 6 ++++-- tools/donate_cpu_lib.py | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index 98fb5f2b3c6..08c22d560d5 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -71,9 +71,11 @@ def test_get_assertion_failures(): assert get_assertion_failures('') == [] assert get_assertion_failures('Checking file.cpp ...\n') == [] glibc_msg = "cppcheck: build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed." - assert get_assertion_failures('Checking file.cpp ...\n' + glibc_msg + '\n') == [glibc_msg] + # the program name is removed + msg = "build/symboldatabase.cpp:6760: bool Variable::arrayDimensions(const Settings&, bool&): Assertion `dimension_.tok->values().size() == 1' failed." + assert get_assertion_failures('Checking file.cpp ...\n' + glibc_msg + '\n') == [msg] # same message twice => reported once - assert get_assertion_failures(glibc_msg + '\n' + glibc_msg + '\n') == [glibc_msg] + assert get_assertion_failures(glibc_msg + '\n' + glibc_msg + '\n') == [msg] # not an assertion failure message assert get_assertion_failures("Assertion `x' failed.\n") == [] assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == [] diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 68bf5b39e2e..892922a6c25 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -439,15 +439,17 @@ def __run_command(cmd, print_cmd=True): # glibc assertion failure message: ": :: : Assertion `' failed." # Example: cppcheck: lib/token.cpp:123: void Token::foo(): Assertion `x' failed. -__assertion_failure_re = re.compile(r"^\S+: [^:\s]+:\d+: .+: Assertion `.+' failed\.$") +__assertion_failure_re = re.compile(r"^\S+: (?P[^:\s]+:\d+: .+: Assertion `.+' failed\.)$") def get_assertion_failures(text): + # The program name is removed from the messages. The servers treat lines that start with "cppcheck: " as + # the version line. messages = [] for line in text.split('\n'): - line = line.strip() - if __assertion_failure_re.match(line) and line not in messages: - messages.append(line) + m = __assertion_failure_re.match(line.strip()) + if m and m.group('message') not in messages: + messages.append(m.group('message')) return messages From 4a349309e2d6bbc29360e5f0a4d322095641ba67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Nov 2026 19:10:11 +0100 Subject: [PATCH 4/4] review 3 --- test/tools/donate_cpu_lib_test.py | 47 +++++++++++++++++++++++++++++++ tools/donate_cpu_lib.py | 7 +++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/test/tools/donate_cpu_lib_test.py b/test/tools/donate_cpu_lib_test.py index 08c22d560d5..cc892518b15 100644 --- a/test/tools/donate_cpu_lib_test.py +++ b/test/tools/donate_cpu_lib_test.py @@ -16,6 +16,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import sys + +import pytest + from donate_cpu_lib import * def _test_library_includes(tmpdir, libs, content, libinc_obj=None): @@ -79,3 +83,46 @@ def test_get_assertion_failures(): # not an assertion failure message assert get_assertion_failures("Assertion `x' failed.\n") == [] assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == [] + +def _write_script(path, content): + with open(path, 'wt') as f: + f.write('#!/usr/bin/env python3\nimport sys\n' + content) + os.chmod(path, 0o755) + +def _scan_package_assertion_failures(tmpdir, monkeypatch, gdb_stdout): + # -j2 run: two child processes crash with an assertion failure + _write_script(os.path.join(str(tmpdir), 'cppcheck'), + 'sys.stderr.write("cppcheck: lib/checkb.cpp:20: void checkB(): Assertion `y\' failed.\\n"\n' + ' "cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n"\n' + ' "src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n"\n' + ' "src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n")\n' + 'sys.exit(1)\n') + # gdb run on src/a.c + _write_script(os.path.join(str(tmpdir), 'gdb'), + 'sys.stdout.write({!r})\n'.format(gdb_stdout) + + 'sys.stderr.write("cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n")\n') + monkeypatch.setenv('PATH', str(tmpdir) + os.pathsep + os.environ['PATH']) + monkeypatch.setattr('donate_cpu_lib.__jobs', '-j2') + returncode, stacktrace, _, _, _, _ = scan_package(str(tmpdir), str(tmpdir), []) + assert returncode == -6 + return stacktrace + +@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb') +def test_scan_package_assertion_failure(tmpdir, monkeypatch): + # only the assertion failure from the gdb run is put in the stack trace + stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, + 'Checking src/a.c ...\n\nProgram received signal SIGABRT, Aborted.\n' + '#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n') + assert stacktrace == 'Checking src/a.c ...\n\n' \ + "lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \ + 'Program received signal SIGABRT, Aborted.\n' \ + '#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n' + +@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb') +def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch): + # no stack trace from gdb => the assertion failures from the first run are used + stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, '') + assert stacktrace == "lib/checkb.cpp:20: void checkB(): Assertion `y' failed.\n" \ + "lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \ + 'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \ + 'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' diff --git a/tools/donate_cpu_lib.py b/tools/donate_cpu_lib.py index 892922a6c25..b62a01ff2e3 100644 --- a/tools/donate_cpu_lib.py +++ b/tools/donate_cpu_lib.py @@ -552,6 +552,7 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, # make sure we have the actual error code set if has_sig: returncode = -sig_num + # assertion failures from the first run, used if there is no stack trace from gdb assertion_failures = get_assertion_failures(stderr) stacktrace = '' if capture_callstack: @@ -562,11 +563,11 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True, else: cmd += dir_to_scan _, st_stdout, st_stderr, _ = __run_command(cmd) - for msg in get_assertion_failures(st_stderr): - if msg not in assertion_failures: - assertion_failures.append(msg) gdb_pos = st_stdout.find(" received signal") if gdb_pos != -1: + # with -jN the first run can have assertion failures from several crashes, gdb only re-runs one file + # => only use the assertion failures from the gdb run + assertion_failures = get_assertion_failures(st_stderr) last_check_pos = st_stdout.rfind('Checking ', 0, gdb_pos) if last_check_pos == -1: stacktrace = st_stdout[gdb_pos:]