Skip to content

Commit df598f3

Browse files
dmcppcheckclaude
andcommitted
donate-cpu: only use the assertion failures from the gdb run in the gdb stack trace
With -jN the first run can have assertion failures from several crashed files, but gdb only re-runs one of them. Messages from other crashes then looked like they belonged to the gdb stack trace. The assertion failures from the first run are now only used when there is no stack trace from gdb. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f6aaa82 commit df598f3

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎test/tools/donate_cpu_lib_test.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import sys
20+
21+
import pytest
22+
1923
from donate_cpu_lib import *
2024

2125
def _test_library_includes(tmpdir, libs, content, libinc_obj=None):
@@ -79,3 +83,46 @@ def test_get_assertion_failures():
7983
# not an assertion failure message
8084
assert get_assertion_failures("Assertion `x' failed.\n") == []
8185
assert get_assertion_failures("file.cpp:1:2: style: Assertion `x' failed. [id]\n") == []
86+
87+
def _write_script(path, content):
88+
with open(path, 'wt') as f:
89+
f.write('#!/usr/bin/env python3\nimport sys\n' + content)
90+
os.chmod(path, 0o755)
91+
92+
def _scan_package_assertion_failures(tmpdir, monkeypatch, gdb_stdout):
93+
# -j2 run: two child processes crash with an assertion failure
94+
_write_script(os.path.join(str(tmpdir), 'cppcheck'),
95+
'sys.stderr.write("cppcheck: lib/checkb.cpp:20: void checkB(): Assertion `y\' failed.\\n"\n'
96+
' "cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n"\n'
97+
' "src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n"\n'
98+
' "src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\\n")\n'
99+
'sys.exit(1)\n')
100+
# gdb run on src/a.c
101+
_write_script(os.path.join(str(tmpdir), 'gdb'),
102+
'sys.stdout.write({!r})\n'.format(gdb_stdout) +
103+
'sys.stderr.write("cppcheck: lib/checka.cpp:10: void checkA(): Assertion `x == 1\' failed.\\n")\n')
104+
monkeypatch.setenv('PATH', str(tmpdir) + os.pathsep + os.environ['PATH'])
105+
monkeypatch.setattr('donate_cpu_lib.__jobs', '-j2')
106+
returncode, stacktrace, _, _, _, _ = scan_package(str(tmpdir), str(tmpdir), [])
107+
assert returncode == -6
108+
return stacktrace
109+
110+
@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb')
111+
def test_scan_package_assertion_failure(tmpdir, monkeypatch):
112+
# only the assertion failure from the gdb run is put in the stack trace
113+
stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch,
114+
'Checking src/a.c ...\n\nProgram received signal SIGABRT, Aborted.\n'
115+
'#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n')
116+
assert stacktrace == 'Checking src/a.c ...\n\n' \
117+
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
118+
'Program received signal SIGABRT, Aborted.\n' \
119+
'#0 0x00007ffff7845b7e in abort () from /lib/x86_64-linux-gnu/libc.so.6\n'
120+
121+
@pytest.mark.skipif(sys.platform == 'win32', reason='uses executable Python scripts as fake cppcheck and gdb')
122+
def test_scan_package_assertion_failure_no_gdb_stacktrace(tmpdir, monkeypatch):
123+
# no stack trace from gdb => the assertion failures from the first run are used
124+
stacktrace = _scan_package_assertion_failures(tmpdir, monkeypatch, '')
125+
assert stacktrace == "lib/checkb.cpp:20: void checkB(): Assertion `y' failed.\n" \
126+
"lib/checka.cpp:10: void checkA(): Assertion `x == 1' failed.\n" \
127+
'src/a.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n' \
128+
'src/b.c:0:0: error: Internal error: Child process crashed with signal 6 [cppcheckError]\n'

‎tools/donate_cpu_lib.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
552552
# make sure we have the actual error code set
553553
if has_sig:
554554
returncode = -sig_num
555+
# assertion failures from the first run, used if there is no stack trace from gdb
555556
assertion_failures = get_assertion_failures(stderr)
556557
stacktrace = ''
557558
if capture_callstack:
@@ -562,11 +563,11 @@ def scan_package(cppcheck_path, source_path, libraries, capture_callstack=True,
562563
else:
563564
cmd += dir_to_scan
564565
_, st_stdout, st_stderr, _ = __run_command(cmd)
565-
for msg in get_assertion_failures(st_stderr):
566-
if msg not in assertion_failures:
567-
assertion_failures.append(msg)
568566
gdb_pos = st_stdout.find(" received signal")
569567
if gdb_pos != -1:
568+
# with -jN the first run can have assertion failures from several crashes, gdb only re-runs one file
569+
# => only use the assertion failures from the gdb run
570+
assertion_failures = get_assertion_failures(st_stderr)
570571
last_check_pos = st_stdout.rfind('Checking ', 0, gdb_pos)
571572
if last_check_pos == -1:
572573
stacktrace = st_stdout[gdb_pos:]

0 commit comments

Comments
 (0)