Repository navigation
Fix #15292 Whole program analysis hangs with --cppcheck-build-dir when a header with nested calls is included by many files - #9093
Conversation
…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>
|
Result on the real project from the ticket (5676 files, Debug|Win32,
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 (Written by Claude Code on behalf of @autoantwort.) |
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerinpr/, andcppcheckinbase/. 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 callstop(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.
|
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/ 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. |
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:
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.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_filesintest/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