Skip to content

Fix #15292 Whole program analysis hangs with --cppcheck-build-dir when a header with nested calls is included by many files - #9093

Open
autoantwort wants to merge 2 commits into
cppcheck-opensource:mainfrom
autoantwort:fix-ctu-findpath-memo
Open

autoantwort wants to merge 2 commits into
cppcheck-opensource:mainfrom
autoantwort:fix-ctu-findpath-memo

Conversation

@autoantwort

@autoantwort autoantwort commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

https://trac.cppcheck.net/ticket/15292

Since #9024 nested calls are loaded from the build dir. With --cppcheck-build-dir, the whole program analysis can now hang when a header with nested calls is included by many files.

Reproducer:

cat > header.h <<'H'
inline int digit(const char *s, int i) { return s[i] - '0'; }
inline int two(const char *s, int i) { return digit(s, i) * 10 + digit(s, i + 1); }
inline int four(const char *s, int i) { return two(s, i) * 100 + two(s, i + 2); }
H
for i in $(seq 1 1000); do printf '#include "header.h"\nint f%d(void) { return four("2026", 0); }\n' $i > test$i.cpp; done
mkdir b
time cppcheck -q -j16 --cppcheck-build-dir=b test*.cpp

Cause: with a build dir every file brings its own copy of the calls in the header, so each header function has N entries in the calls map. findPath() tries all of them on every level and does not remember searches that failed. One unsafe usage costs about N² steps with the default --max-ctu-depth=2, and there is one unsafe usage per file.

Fix: findPath() remembers the (callId, callArgNr, index) states it has visited during one search. Within one search the result of a state only depends on these three values, and a success ends the whole search, so a repeated state has failed before and can be skipped. The paths that are found and reported do not change.

N files before #9024 main this PR
250 0.1 s 0.5 s 0.1 s
500 0.2 s 3.5 s 0.2 s
1000 0.3 s 27.3 s 0.4 s
4000 3.3 s

On the real project from the ticket (5676 files, one header included by 4434 files) the whole program analysis did not finish within 2.5 hours on main. With this PR it takes 38 s (6.6 s before #9024) and reports the same results as before #9024, see the comment below.

Test: test_slow_ctu_header_included_by_many_files in test/cli/performance_test.py (100 files, a header with a chain of 5 functions, --max-ctu-depth=4): 0.13 s with this PR, hits the 20 s timeout without it.

Limitations: the remaining cost is still quadratic in the number of files that include the header (N unsafe usages × N entries in the calls map). Deduplicating identical calls and unsafe usages from different files would remove that, but is not part of this PR.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

autoantwort and others added 2 commits November 30, 2026 17:56
…en a header with nested calls is included by many files)

With a build dir every file brings its own copy of the calls in a
header, so findPath() tried each of them on every level and searched
the same failed states again and again. Remember the visited states
during one search.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test with 2000 files hit the timeout on slow CI runners even with
the fix. 100 files with --max-ctu-depth=4 take 0.1 s with the fix and
more than a minute without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@autoantwort

Copy link
Copy Markdown
Contributor Author

Result on the real project from the ticket (5676 files, Debug|Win32, --enable=all --check-level=exhaustive, --cppcheck-build-dir):

before #9024 (b3d8256^) main this PR
whole program analysis 6.6 s did not finish within 2.5 h 38 s
ctuOneDefinitionRuleViolation 138 138
ctunullpointer / ctuuninitvar / ctuArrayIndex 0 0

The remaining difference to before #9024 comes from the nested calls that are now loaded and the quadratic part mentioned in the description. The other differences in the results (missingReturn, uninitStructMember) come from other commits between b3d8256^ and main.

The first version of the performance test (2000 files) hit the timeout on the slower CI runners even with the fix. It now uses 100 files with a deeper call chain and --max-ctu-depth=4: 0.13 s with this PR, timeout without it.

(Written by Claude Code on behalf of @autoantwort.)

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no problems. Memoizing the visited states is sound. Within one getErrorPath() call, unsafeValue, invalidValue, warning, maxCtuDepth and callsMap stay the same. So whether findPath() succeeds depends only on (callId, callArgNr, index). A state that comes up again must already have failed, because a success returns at once all the way up. path[] is written only when the search unwinds after a success, so a skipped state cannot leave stale entries in it. Each getErrorPath() call gets a fresh visited set, so the separate searches do not affect each other. In my tests the reported results and error paths are the same as on base, and the slowdown from the ticket is gone.

Testing

  • Built cppcheck and testrunner in pr/, and cppcheck in base/. Both builds succeeded.
  • ./testrunner TestCtu TestNullPointer TestUninitVar TestBufferOverrun: 0 failures.
  • pytest performance_test.py -k ctu: the new test passed in 1.0 s. pytest other_test.py -k "ctu or build_dir or builddir": 20 passed, 1 skipped, 1 xfailed.
  • Results unchanged: 60 files include a header with a 3-level chain top -> mid -> deref. One file calls top(0, 0) and the others pass a valid pointer. I ran this with and without --cppcheck-build-dir, at --max-ctu-depth=4. The output is the same on base and the PR, including the full callstack [test37.cpp:2] -> [header.h:3] -> [header.h:2] -> [header.h:1]:ctunullpointer.
  • Speed: I ran the reproducer from the description with 500 files, -j8 --cppcheck-build-dir. Base took 7.0 s and the PR 0.63 s. Neither reported any findings.

Automated review of 68812d6 by Claude. The pull request was built and tested locally.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 68812d6 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9093/
packages: 200
Differing warnings: 0 (+0 -0)
Timing: +1.4% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9093/2026-11-30_18-06-55-68812d62e945_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants